HTML+Css+JavaScript打造一个简单的语音系统
无聊写了一个html页面的语音系统(第一次写网页),其实也算不上系统,就跟vbs差不多,vbs是输入CreateObject(“SAPI.SpVoice”).Speak "此处填写你想让电脑说的话"保存为vbs,而这个是可以看见页面效果的的,注释也写得很详细了
下面贴上代码,直接复制保存就可以用
<!DOCTYPE html>
<html>
<head>
<title>myweb</title>
<style>
html{
height: 100%;
background-color: gray;
}
div{
width: 900px;
margin: 100px auto 0;
}
.input_box{
width: 100%;
height: 380px;
outline: none; /*去掉轮廓线*/
border: 10px solid #000;
box-sizing: border-box;/*怪异盒模型*/
resize: none;/*禁止拖拽*/
border-radius: 20px;/*圆角*/
padding: 20px;
background-color: rgba(255,255,255,5);
font-size: 20px;
}
.speech{
height: 55px;
width: 100%;
font-size: 18px;
outline: none;
border: none;
background-color:#000;
color: #fff;
cursor: pointer;/*鼠标样式,小手*/
border-radius: 20px;
}
.speech:hover{/*hover伪类选择器 当鼠标滑过speech改变背景颜色*/
background-color: #ea4c89
}
</style>
</head>
<body>
<div>
<textarea class="input_box" placeholder="请输入文字......"></textarea>
<input class="speech" type="submit" value="立刻朗读">
</div>
<script>/*JavaScript代码*/
function speeching(inputBox,speechBtn) {
var inputBox = document.querySelector(inputBox),
speechBtn = document.querySelector(speechBtn);
var inputText = '';
speechBtn.onclick = function(){
var message = null;/*默认文本框为空*/
var text = inputBox.value;/*接收按钮得到的值*/
message = new SpeechSynthesisUtterance(text);/*传给message*/
window.speechSynthesis.speak(message);/*利用电脑本身应用读取输入内容*/
}
}
</script>
<script>
speeching('.input_box','.speech');
</script>
</body>
</html>
效果图:
由于它念什么这里就不展示了
正在尝试写博客,把会的分享给你们,如有写的不好的地方,希望指点一下,谢谢!