本文实例为大家分享了JavaScript实现淘宝商品广告效果的具体代码,供大家参考,具体内容如下
CSS部分:
ul{ margin: 0; padding: 0; } li{ list-style: none; } #ad{ width: 298px; height: 208px; border: 1px #ff6300 solid; padding: 4px 1px; text-align: center; } #ad .listL{ float: left; } #ad .listR{ float: right; } #ad li{ width: 48px; height: 26px; border: 1px #ffadad solid; background: #fff7f7; color: #333; line-height: 26px; margin-bottom: 2px; cursor: pointer; } #ad img{ height: 206px; width: 188px; background: url(images/loader_ico.webp) no-repeat center center; } #ad .cur{ background: #ff8494; color: #fff }
HTML部分:
<div id="ad"> <ul class="listL"> <!-- <li class="cur"></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> --> </ul> <a href="#"><img src="" alt=""></a> <ul class="listR"> <!-- <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> --> </ul> </div>
JS部分:
window.onload = function(){ var oDiv = document.getElementById('ad'); var aUl = oDiv.getElementsByTagName('ul'); var oImg = oDiv.getElementsByTagName('img')[0]; var aImg = ['images/pic1.webp','images/pic2.webp','images/pic3.webp','images/pic4.webp','images/pic5.webp','images/pic6.webp','images/pic7.webp','images/pic8.webp','images/pic1.webp','images/pic2.webp','images/pic3.webp','images/pic4.webp','images/pic5.webp','images/pic6.webp']; var aTxt = ['连衣裙','T恤','雪纺','铅笔裤','婚纱','外套','连体裤','包包','凉鞋','单鞋','太阳镜','丝袜','帆布鞋','情侣鞋']; var len = aImg.length; var oldNum = 0; var num = 0; var timer = null; var speed = 1; // 创建添加左右两侧li for( var i = 0; i < len/2; i++){ aUl[0].innerHTML += '<li>'+ aTxt[i] +'</li>' aUl[1].innerHTML += '<li>'+ aTxt[i + len/2] +'</li>' } var aLiL = aUl[0].getElementsByTagName('li'); var aLiR = aUl[1].getElementsByTagName('li'); var arrLi = []; // 将遍历的所有li添加到数组arrLi中 for( var i = 0; i < aLiL.length; i++){ arrLi.push(aLiL[i]); } for( var i = 0; i < aLiR.length; i++){ arrLi.push(aLiR[i]); } // console.log(arrLi.length); // 函数初始化 function init(n){ oImg.src = aImg[n]; arrLi[oldNum].className = ''; arrLi[n].className = 'cur'; oldNum = n; } init(0); // 鼠标经过li,图片切换 for(var i = 0; i < len; i++){ arrLi[i].index = i; arrLi[i].onmouseover = function(){ init(this.index); } }; // 定时切换 function fnTimer(n){ timer = setInterval(function(){ // type1:顺序切换 /* n ++; if(n == len){ n = 0; }*/ // type2:倒序切换 if(n == len-1){ speed = -1; }else if(n== 0){ speed = 1; } n += speed; init(n); },1000); }; fnTimer(0); // 鼠标移入,清除定时器 oDiv.onmouseover = function(){ clearInterval(timer); }; // 鼠标移出,开启定时器 oDiv.onmouseout = function(){ fnTimer(oldNum); }; };
预览效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。