- 从三月底开始,慢慢地上手了JavaScript,去年在学校放假前也看了一套视频,总感觉好像是会了又好像不会,现在看了Blue老师的视频,感觉思路清晰多了,也终于算是入门了吧。
- 接下来我会把学习过程中跟着视频写的一些案例的代码分享给大家,供大家参考。
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>轮播动图</title> <link rel="stylesheet" type="text/css" href="zns_style.css"> <script src="move.js"></script> <script> function getByClass(oParent,sClass){
var aEle=oParent.getElementsByTagName("*"); var aResult=[]; for(var i=0;i<aEle.length;i++){
if(aEle[i].className==sClass){
aResult.push(aEle[i]); } } return aResult; } window.onload=function() {
var odiv = document.getElementById("playimage"); var obtnprev = getByClass(odiv, "prev")[0]; var obtnnext = getByClass(odiv, "next")[0]; var omarkleft = getByClass(odiv, "mark_left")[0]; var omarkright = getByClass(odiv, "mark_right")[0]; var odivsmall = getByClass(odiv, "small_pic")[0]; var oulsmall=odivsmall.getElementsByTagName("ul")[0]; var alismall = odivsmall.getElementsByTagName("li"); var oulbig = getByClass(odiv, "big_pic")[0]; var alibig = oulbig.getElementsByTagName("li"); var nowzindex = 2; var now = 0; oulsmall.style.width=alismall.length*alismall[0].offsetWidth+"px"; //左右按钮 obtnprev.onmouseover = omarkleft.onmouseover = function () {
//鼠标移入左边,图标显示出来 startMove(obtnprev, "opacity", 100); }; obtnprev.onmouseout = omarkleft.onmouseout = function () {
startMove(obtnprev, "opacity", 0); }; obtnnext.onmouseover = omarkright.onmouseover = function () {
startMove(obtnnext, "opacity", 100); }; obtnnext.onmouseout = omarkright.onmouseout = function () {
startMove(obtnnext, "opacity", 0); }; //大图切换 for (var i = 0; i < alismall.length; i++) {
alismall[i].index = i; alismall[i].onclick = function () {
if (this.index == now)return; now = this.index; tab(); }; alismall[i].onmouseover = function () {
startMove(this, "opacity", 100); }; alismall[i].onmouseout = function () {
if (this.index != now) {
startMove(this, "opacity", 60); } }; } function tab(){
alibig[now].style.zIndex = nowzindex++; for (var i = 0; i < alismall.length; i++) {
startMove(alismall[i], "opacity", 60); } startMove(alismall[now], "opacity", 100); alibig[now].style.height = 0; startMove(alibig[now], "height", 320); if(now==0){
startMove(oulsmall,"left",0) }else if(now==alismall.length-1){
startMove(oulsmall,"left",-(now-2)*alismall[0].offsetWidth); }else {
startMove(oulsmall, "left", -(now - 1) * alismall[0].offsetWidth); } } obtnprev.onclick=function(){
now--; if(now==-1){
now=alismall.length-1; } tab(); }; obtnnext.onclick=function(){
now++; if(now==alismall.length){
now=0; } tab(); }; var timer= setInterval(obtnnext.onclick,2000); odiv.onmouseover=function(){
//当鼠标移入轮播暂停 clearInterval(timer); }; odiv.onmouseout=function(){
//当鼠标移出继续轮播 timer=setInterval(obtnnext.onclick,2000); }; } </script> </head> <body> <div id="playimage" class="play"> <ul class="big_pic"> <div class="prev"></div> <div class="next"></div> <div class="text">加载图片说明……</div> <div class="length">计算图片数量……</div> <a class="mark_left" href="javascript:;"></a> <a class="mark_right" href="javascript:;"></a> <div class="bg"></div> <li style="z-index:1;"><img src="image/休闲.jpg"> </li> <li><img src="image/室内复古1.jpg"> </li> <li><img src="image/室内复古2.jpg"> </li> <li><img src="image/室内复古3.jpg"> </li> <li><img src="image/室内田园.jpg"> </li> </ul> <div class="small_pic"> <ul style="width: 390px;"> <li style="opacity: 1;"><img src="image/休闲.jpg"> </li> <li><img src="image/室内复古1.jpg"> </li> <li><img src="image/室内复古2.jpg"> </li> <li><img src="image/室内复古3.jpg"> </li> <li><img src="image/室内田园.jpg"> </li> </ul> </div> </div> </body> </html>`
讯享网
move.js

讯享网function getStyle(obj,name){
if(obj.currentStyle){
return obj.currentStyle[name]; }else{
return getComputedStyle(obj,false)[name]; } } function startMove(obj,attr,iTarget){
clearInterval(obj.timer); obj.timer=setInterval(function(){
var cur=0; if(attr=="opacity"){
cur=parseFloat(getStyle(obj,attr))*100; }else{
cur=parseInt(getStyle(obj,attr)); } var speed=(iTarget-cur)/6; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(cur==iTarget){
clearInterval(obj.timer); }else{
if(attr=="opacity"){
obj.style.filter="alpha(opacity:"+(cur+speed)+")"; obj.style.opacity=(cur+speed)/100; }else {
obj.style[attr] = cur + speed + "px"; } } },30); }
zns_style.css
body{
background: #666;} ul{
padding: 0;margin: 0;} li{
list-style: none;} img{
border: 0;} .play{
width: 400px;height:430px;margin: 50px auto 0;background: #999;font:12px Arial;} .big_pic {
width:400px;height: 320px;overflow: hidden;border-bottom: 1px solid #ccc;background: #222;position: relative;} .big_pic li{
width: 400px;height:320px;overflow: hidden;position: absolute;top:0;left:0;z-index: 0;background: url(image/室内夕阳.jpg) no-repeat center center;} .mark_left{
width:200px;height:320px;position: absolute;left:0;top:0;background: red;filter: alpha(opacity:0);opacity: 0;z-index: 3000;} .mark_right{
width:200px;height: 320px;position: absolute;left:200px;top:0;background: green;filter: alpha(opacity:0);opacity: 0;z-index: 3000;} .big_pic .prev{
width: 60px;height: 60px;background: url(image/left.png) no-repeat;position: absolute;top:130px;left:10px;z-index: 3001;filter: alpha(opacity:0);opacity:0;cursor: pointer;} .big_pic .next{
width: 60px;height: 60px;background: url(image/right.png) no-repeat;position: absolute;top:130px;right:10px;z-index: 3001;filter: alpha(opacity:0);opacity:0;cursor: pointer;} .big_pic .text{
position: absolute;left:10px;top:302px;z-index: 3000;color: #ccc;} .big_pic .length{
position: absolute;right: 10px;bottom: 4px;z-index: 3000;color:#ccc;} .big_pic .bg{
width:400px;height:25px;background: #000;filter: alpha(opacity=60);opacity: 0.6;position: absolute;z-index: 2999;bottom: 0;left:0;} .small_pic{
width:380px;height:94px;position: relative;top:7px;left:10px;overflow: hidden;} .small_pic ul{
height: 94px;position: absolute;top:0;left:0; } .small_pic li{
width:120px;height:94px;float: left;padding-right: 10px;background: url("image/室内田园.jpg") no-repeat center center;cursor: pointer;filter: alpha(opacity=60);opacity: 0.6;} .small_pic img{
width: 120px;height:94px;}
效果图


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/82333.html