Home > Web Front-end > JS Tutorial > body text

Detailed explanation of how JavaScript implements Taobao product advertising effects

黄舟
Release: 2017-08-11 10:48:20
Original
1777 people have browsed it

This article mainly introduces in detail the effect of Taobao product advertising based on JavaScript. It has certain reference value. Interested friends can refer to it.

The example in this article shares with everyone the implementation of Taobao using JavaScript. The specific code for the product advertising effect is for your reference. The specific content is as follows

CSS part:


##

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.gif) no-repeat center center; } 
  #ad .cur{ background: #ff8494; color: #fff }
Copy after login

HTML part:



<p 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> 
 </p>
Copy after login

JS part:



window.onload = function(){ 
  var op = document.getElementById(&#39;ad&#39;); 
  var aUl = op.getElementsByTagName(&#39;ul&#39;); 
  var oImg = op.getElementsByTagName(&#39;img&#39;)[0]; 
  var aImg = [&#39;images/pic1.jpg&#39;,&#39;images/pic2.jpg&#39;,&#39;images/pic3.jpg&#39;,&#39;images/pic4.jpg&#39;,&#39;images/pic5.png&#39;,&#39;images/pic6.png&#39;,&#39;images/pic7.png&#39;,&#39;images/pic8.png&#39;,&#39;images/pic1.jpg&#39;,&#39;images/pic2.jpg&#39;,&#39;images/pic3.jpg&#39;,&#39;images/pic4.jpg&#39;,&#39;images/pic5.png&#39;,&#39;images/pic6.png&#39;]; 
  var aTxt = [&#39;连衣裙&#39;,&#39;T恤&#39;,&#39;雪纺&#39;,&#39;铅笔裤&#39;,&#39;婚纱&#39;,&#39;外套&#39;,&#39;连体裤&#39;,&#39;包包&#39;,&#39;凉鞋&#39;,&#39;单鞋&#39;,&#39;太阳镜&#39;,&#39;丝袜&#39;,&#39;帆布鞋&#39;,&#39;情侣鞋&#39;]; 
  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 += &#39;<li>&#39;+ aTxt[i] +&#39;</li>&#39; 
   aUl[1].innerHTML += &#39;<li>&#39;+ aTxt[i + len/2] +&#39;</li>&#39; 
  } 
 
  var aLiL = aUl[0].getElementsByTagName(&#39;li&#39;); 
  var aLiR = aUl[1].getElementsByTagName(&#39;li&#39;); 
  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 = &#39;&#39;; 
   arrLi[n].className = &#39;cur&#39;; 
   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); 
 
  // 鼠标移入,清除定时器 
  op.onmouseover = function(){ 
   clearInterval(timer); 
  }; 
 
  // 鼠标移出,开启定时器 
  op.onmouseout = function(){ 
   fnTimer(oldNum); 
  }; 
  };
Copy after login

Preview effect:

The above is the detailed content of Detailed explanation of how JavaScript implements Taobao product advertising effects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template