国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

原生js和jQuery實現淡入淡出輪播效果

2019-11-20 10:55:56
字體:
來源:轉載
供稿:網友

本文實例為大家介紹了基于jQuery實現淡入淡出輪播效果的關鍵代碼,分享給大家供大家參考,具體內容如下:
基本原理:將所有圖片絕對定位在同一位置,透明度設為0,然后通過jQuery的淡入淡出實現圖片的切換效果。
html代碼:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>一個輪播</title><style> #scrollPlay{ width: 730px; height: 336px; /*overflow: hidden;*/ } #pre{ position: absolute; margin-top: 150px; width:30px; height: 30px; background: #000; color:#fff; opacity: 0.7; text-align: center; line-height: 30px; font-size: 20px; z-index: 10; } img{ opacity: 0; position: absolute; } #next{ position: absolute; margin-left:700px; margin-top: 150px; width:30px; height: 30px; background: #000; color:#fff; opacity: 0.7; text-align: center; line-height: 30px; font-size: 20px; z-index: 10; } span{ display: block; width: 15px; height: 15px; float: left; border: 1px solid #fff; } #buttons{ position: absolute; background: #000; margin-top: 300px; margin-left: 300px; z-index: 10; } .onactive{ background: green; }</style><script src='jquery.js'></script><script src='index.js'></script></head><body> <div id='scrollPlay'> <div id='buttons'>  <span index = 0 class='onactive'></span>  <span index = 1></span>  <span index = 2></span>  <span index = 3></span>  <span index = 4></span> </div> <div id='pre'><</div> <div id='next'>></div> <img src='images/1.jpg' alt='pics' style='opacity:1'> <img src='images/2.jpg' alt='pics'> <img src='images/3.jpg' alt='pics'> <img src='images/4.jpg' alt='pics'> <img src='images/5.jpg' alt='pics'> </div></body> </html>

JS:

$(function(){ var index = 0; var flag = false; //用于判斷是否處于動畫狀態 //切換函數 function move(offset){  flag=true; //console.log(offset) $('img').eq(index).fadeOut('slow',function(){  if(offset>0){  if(index ==4){   index=0;   }else{   //console.log(index);   index=index+offset;   //console.log(index);  }  }  if(offset<0){  if(index==0){  index=4;  }else{  index=index+offset  }  }  $('img').eq(index).fadeTo('slow',1) //使用fadeIn不成功:$('img').eq(index).fadeIn('slow')  showButton();  flag=false; });  } //點擊切換上一張 $('#pre').click(function(){ if(flag==false){  move(-1) }  }) //點擊切換下一張 $('#next').click(function(){ if(flag==false){  move(1) } }) //點擊按鈕直接切換 $('span').click(function(){ if(flag==false){  var i= $(this).attr('index')  //console.log(i)  //console.log(index)  //console.log(i-index)  move(i-index)   showButton(); }  })  //高亮顯示按鈕 function showButton(){ if($('span').hasClass('onactive')){  $('span').removeClass(); } $('span').eq(index).addClass('onactive') } //自動播放 var timer; function go(){ timer = setInterval(function(){  $('#next').trigger('click'); },3000) } //鼠標懸停,清除自動播放 $('#scrollPlay').mouseover(function(){  clearInterval(timer) }) //鼠標移開,開始自動播放 $('#scrollPlay').mouseout(function(){  go(); }) go(); })

文章最后為大家提了一個小問題,希望大家能給出解決方法。
使用fadeIn淡入時卻無效果,最后只能使用fadeTo實現,這是什么原因?
為大家分享一個小例子:原生JS實現淡入淡出效果(fadeIn/fadeOut/fadeTo)
淡入淡出效果,在日常項目中經常用到,可惜原生JS沒有類似的方法,而有時小的頁面并不值得引入一個jQuery庫,所以就自己寫了一個,已封裝, 有用得著的朋友, 可以直接使用. 代碼中另附有一個設置元素透明度的方法, 是按IE規則(0~100)設置, 若改成標準設置方法(0.00~1.00), 下面使用時請考慮浮點精確表達差值.
參數說明:
fadeIn()與fadeOut()均有三個參數,第一個是事件, 必填; 第二個是淡入淡出速度, 正整數, 大小自己權衡, 可選參數; 第三個, 是指定淡入淡出到的透明度值(類似于jQuery中的fadeTo()), 0~100的正整數值, 也是可選參數.

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>原生JS實現淡入淡出效果</title> <style> /*demo css*/ #demo div.box {float:left;width:31%;margin:0 1%} #demo div.box h2{margin-bottom:10px} #demo div.box h2 input{padding:5px 8px;font-size:14px;font-weight:bolder} #demo div.box div{text-indent:10px; line-height:22px;border:2px solid #555;padding:0.5em;overflow:hidden} </style> <script> window.onload = function(){  //底層共用  var iBase = {  Id: function(name){   return document.getElementById(name);  },  //設置元素透明度,透明度值按IE規則計,即0~100  SetOpacity: function(ev, v){   ev.filters ? ev.style.filter = 'alpha(opacity=' + v + ')' : ev.style.opacity = v / 100;  }  }  //淡入效果(含淡入到指定透明度)  function fadeIn(elem, speed, opacity){  /*   * 參數說明   * elem==>需要淡入的元素   * speed==>淡入速度,正整數(可選)   * opacity==>淡入到指定的透明度,0~100(可選)   */  speedspeed = speed || 20;  opacityopacity = opacity || 100;  //顯示元素,并將元素值為0透明度(不可見)  elem.style.display = 'block';  iBase.SetOpacity(elem, 0);  //初始化透明度變化值為0  var val = 0;  //循環將透明值以5遞增,即淡入效果  (function(){   iBase.SetOpacity(elem, val);   val += 5;   if (val <= opacity) {   setTimeout(arguments.callee, speed)   }  })();  }   //淡出效果(含淡出到指定透明度)  function fadeOut(elem, speed, opacity){  /*   * 參數說明   * elem==>需要淡入的元素   * speed==>淡入速度,正整數(可選)   * opacity==>淡入到指定的透明度,0~100(可選)   */  speedspeed = speed || 20;  opacityopacity = opacity || 0;  //初始化透明度變化值為0  var val = 100;  //循環將透明值以5遞減,即淡出效果  (function(){   iBase.SetOpacity(elem, val);   val -= 5;   if (val >= opacity) {   setTimeout(arguments.callee, speed);   }else if (val < 0) {   //元素透明度為0后隱藏元素   elem.style.display = 'none';   }  })();  }    var btns = iBase.Id('demo').getElementsByTagName('input');   btns[0].onclick = function(){  fadeIn(iBase.Id('fadeIn'));  }  btns[1].onclick = function(){  fadeOut(iBase.Id('fadeOut'),40);  }  btns[2].onclick = function(){  fadeOut(iBase.Id('fadeTo'), 20, 10);  }  } </script> </head> <body>  <!--DEMO start--> <div id="demo">  <div class="box">  <h2><input type="button" value="點擊淡入" /></h2>  <div id="fadeIn" style="display:none">   <p>武林網</p>   <p>m.survivalescaperooms.com</p>  </div>  <p>武林網是國內專業的網站建設資源.</p>  </div>   <div class="box">  <h2><input type="button" value="點擊淡出" /></h2>  <div id="fadeOut">   <p>武林網</p>   <p>m.survivalescaperooms.com</p>  </div>  <p>武林網是國內專業的網站建設資源.</p>  </div>   <div class="box">  <h2><input type="button" value="點擊淡出至指定透明度" /></h2>  <div id="fadeTo">    </div>  <p>武林網是國內專業的網站建設資源.</p>  </div> </div> <!--DEMO end-->  </body> </html> 

以上就是本文的全部內容,希望對大家學習原生js和jQuery實現淡入淡出輪播效果有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤阳县| 江陵县| 文化| 延庆县| 德昌县| 岳西县| 财经| 黎川县| 柯坪县| 商都县| 梅州市| 噶尔县| 三江| 柳州市| 禹城市| 灵寿县| 平原县| 西乡县| 温宿县| 斗六市| 海阳市| 报价| 黄梅县| 云阳县| 太原市| 临海市| 柞水县| 泰州市| 阿坝县| 敖汉旗| 上虞市| 镇赉县| 门头沟区| 阿拉善右旗| 左贡县| 茌平县| 泌阳县| 张北县| 乌拉特前旗| 五台县| 海阳市|