代码:
汽车相册:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>汽车相册</title> <style> .box { /*宽度*/ width: 600px; /*高度*/ height: 700px; /*背景色*/ background-color: #efefef; /*边框*/ border: 1px solid lightgray; /*外边距*/ margin: 20px auto; /*文本位置*/ text-align: center; /*文本颜色*/ color: #636363; /*阴影*/ box-shadow: 2px 2px 2px #999; } .box ul { /*外边距*/ margin: 0; /*内边距*/ padding: 0; /*去除浮动影响*/ overflow: hidden; } .box ul li { /*设置列表项标记的类型*/ list-style-type: none; /*浮动*/ float: left; /*背景色*/ background-color: skyblue; /*左侧外边距*/ margin-left: 35px; } .box ul li a { /*将a转为块元素,并设置为li的宽高,这样可以使整个li可以被点击*/ display: block; width: 105px; height: 40px; /*文本垂直居中*/ line-height: 40px; color: white; /*去掉下划线*/ text-decoration:none; } /*鼠标指针悬浮元素变化*/ .box ul li:hover { /*鼠标指针悬浮字体变大*/ font-size: 1.1em; /*鼠标指针悬浮背景色改变*/ background-color: pink; } .box .pic { width: 550px; height: 450px; border: 1px solid lightgray; /*消除img标签底部的空间区*/ line-height: 1px; margin: auto; margin-top: 50px; } .box .pic img { width: 100%; height: 100%; } .box .pic img:hover { /*透明度*/ opacity: 0.9; } </style> </head> <body> <div class="box"> <h2>汽车小百科</h2> <ul> <li> <a href="../images/minicooper.jpg" title="动力:1.2T,百公里油耗:5.3L,售价:18.50万。" onclick="changepic(this);return false">minicooper</a> </li> <li> <a href="../images/路虎.jpg" title="动力:2.0T,百公里油耗:11L,售价:69.80万。" onclick="changepic(this);return false">路虎</a> </li> <li> <a href="../images/保时捷.jpg" title="动力:3.0T,百公里油耗:7.8L,售价:131.80万。" onclick="changepic(this);return false">保时捷</a> </li> <li> <a href="../images/宾利.jpg" title="动力:4.0T,百公里油耗:10.6L,售价:288.00万。" onclick="changepic(this);return false">宾利</a> </li> </ul> <div class="pic"> <img src="../images/heirenwenhao.jpg" alt="" id="img"> </div> <p id="info"></p> </div> <script> function changepic(pic) { //1.获取到要替换的明星图片与简介信息 var picUrl = pic.href var picInfo = pic.title var picName = pic.innerHTML //2.获取到页面中,要被替换掉的图像元素对象 var img = document.getElementById('img') var p = document.getElementById('info') //3. 将对应的图像与信息占位符进行替换 img.src = picUrl p.innerHTML = picName+':'+picInfo p.innerHTML = '<span style="color:coral">'+picName+':'+picInfo+'</span>' } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
计算器:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>迷你计算器</title> <style type="text/css"> .box { width: 500px; height: 200px; background-color: #efefef; border: 1px solid lightgray; text-align: center; /*外边距*/ margin: auto; margin: 30px auto; color: #636363; /*圆角*/ border-radius: 55px; /*阴影*/ box-shadow: 2px 2px 2px #999; } table { margin: auto; } td { width: 100px; height: 30px; /*内边距*/ padding: 5px 10px; } input, select { width: 100%; height:100%; border:none; text-align: center; } button { width: 100%; height: 100%; border: none; background-color: skyblue; color: white; } /*鼠标指针悬浮元素变化*/ button:hover { /*指针变小手*/ cursor: pointer; background-color: pink; width: 105%; height: 105%; } </style> </head> <body> <div class="box"> <h2>迷你计算器</h2> <form> <table> <tr> <td><input type="text" name="opt1" placeholder="操作数1"></td> <td><select name="option"> <option value="null">请选择操作</option> <option value="add">+</option> <option value="sub">-</option> <option value="mul">*</option> <option value="div">/</option> </select> </td> <td><input type="text" name="opt2" placeholder="操作数2"></td> <td><button type="button">计算</button></td> </tr> <tr> <td colspan="2" align="right"><h3>结果:</h3></td> <td colspan="2" align="left"><h3 id="placeholder"></h3></td> </tr> </table> </form> </div> <script type="text/javascript"> //1.获取操作数,按钮与结果占位符 var opt1 = document.getElementsByName('opt1')[0] var opt2 = document.getElementsByName('opt2')[0] var opt = document.getElementsByName('option')[0] var btn = document.getElementsByTagName('button')[0] var placeholder = document.getElementById('placeholder') //2.给按钮添加事件,执行计算操作 btn.onclick = function(){ if (opt1.value.length == 0 ) { alert('第一个操作数不能为空') opt1.focus() return false } else if (isNaN(opt1.value)) { alert('第一个操作数必须为数字') opt1.focus() return false } else if (opt2.value.length == 0) { alert('第二个操作数不能为空') opt2.focus() return false } else if (isNaN(opt2.value)) { alert('第二个操作数必须为数字') opt2.focus() return false } else { var data1 = parseFloat(opt1.value) var data2 = parseFloat(opt2.value) } var option = opt.value var temp = 0 var flag = '' var result = '' switch (option){ case 'null': alert("请选择操作类型") opt.focus() return false case 'add': flag = '+' temp = data1 + data2 break case 'sub': flag = '-' temp = data1 - data2 break case 'mul': flag = '*' temp = data1 * data2 break case 'div': flag = '/' if (data2 == 0) { alert('除数不能为0,请重新输入') opt2.focus() return false } else { temp = data1 / data2 } break } var str = '<span style="color:coral">' str += data1+' '+flag+' '+data2 + ' = ' + temp str += '</span>' placeholder.innerHTML = str } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
手抄: