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

首頁 > 開發(fā) > JS > 正文

原生js和jQuery實現(xiàn)淡入淡出輪播效果

2024-05-06 16:26:51
字體:
供稿:網(wǎng)友
這篇文章主要介紹了原生js和jQuery實現(xiàn)淡入淡出輪播效果,介紹到了jQuery實現(xiàn)淡入淡出輪播效果的基本原理,感興趣的小伙伴們可以參考一下
 

本文實例為大家介紹了基于jQuery實現(xiàn)淡入淡出輪播效果的關(guān)鍵代碼,分享給大家供大家參考,具體內(nèi)容如下:
基本原理:將所有圖片絕對定位在同一位置,透明度設(shè)為0,然后通過jQuery的淡入淡出實現(xiàn)圖片的切換效果。
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' <img src='images/2.jpg' <img src='images/3.jpg' <img src='images/4.jpg' <img src='images/5.jpg' </div></body> </html>

JS:

$(function(){ var index = 0; var flag = false; //用于判斷是否處于動畫狀態(tài) //切換函數(shù) 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實現(xiàn),這是什么原因?
為大家分享一個小例子:原生JS實現(xiàn)淡入淡出效果(fadeIn/fadeOut/fadeTo)
淡入淡出效果,在日常項目中經(jīng)常用到,可惜原生JS沒有類似的方法,而有時小的頁面并不值得引入一個jQuery庫,所以就自己寫了一個,已封裝, 有用得著的朋友, 可以直接使用. 代碼中另附有一個設(shè)置元素透明度的方法, 是按IE規(guī)則(0~100)設(shè)置, 若改成標準設(shè)置方法(0.00~1.00), 下面使用時請考慮浮點精確表達差值.
參數(shù)說明:
fadeIn()與fadeOut()均有三個參數(shù),第一個是事件, 必填; 第二個是淡入淡出速度, 正整數(shù), 大小自己權(quán)衡, 可選參數(shù); 第三個, 是指定淡入淡出到的透明度值(類似于jQuery中的fadeTo()), 0~100的正整數(shù)值, 也是可選參數(shù).

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>原生JS實現(xiàn)淡入淡出效果</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);  },  //設(shè)置元素透明度,透明度值按IE規(guī)則計,即0~100  SetOpacity: function(ev, v){   ev.filters ? ev.style.filter = 'alpha(opacity=' + v + ')' : ev.style.opacity = v / 100;  }  }  //淡入效果(含淡入到指定透明度)  function fadeIn(elem, speed, opacity){  /*   * 參數(shù)說明   * elem==>需要淡入的元素   * speed==>淡入速度,正整數(shù)(可選)   * opacity==>淡入到指定的透明度,0~100(可選)   */  speedspeed = speed || 20;  opacityopacity = opacity || 100;  //顯示元素,并將元素值為0透明度(不可見)  elem.style.display = 'block';  iBase.SetOpacity(elem, 0);  //初始化透明度變化值為0  var val = 0;  //循環(huán)將透明值以5遞增,即淡入效果  (function(){   iBase.SetOpacity(elem, val);   val += 5;   if (val <= opacity) {   setTimeout(arguments.callee, speed)   }  })();  }   //淡出效果(含淡出到指定透明度)  function fadeOut(elem, speed, opacity){  /*   * 參數(shù)說明   * elem==>需要淡入的元素   * speed==>淡入速度,正整數(shù)(可選)   * opacity==>淡入到指定的透明度,0~100(可選)   */  speedspeed = speed || 20;  opacityopacity = opacity || 0;  //初始化透明度變化值為0  var val = 100;  //循環(huán)將透明值以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>VeVb武林網(wǎng)</p>   <p>m.survivalescaperooms.com</p>  </div>  <p>VeVb武林網(wǎng)是國內(nèi)專業(yè)的網(wǎng)站建設(shè)資源.</p>  </div>   <div class="box">  <h2><input type="button" value="點擊淡出" /></h2>  <div id="fadeOut">   <p>VeVb武林網(wǎng)</p>   <p>m.survivalescaperooms.com</p>  </div>  <p>VeVb武林網(wǎng)是國內(nèi)專業(yè)的網(wǎng)站建設(shè)資源.</p>  </div>   <div class="box">  <h2><input type="button" value="點擊淡出至指定透明度" /></h2>  <div id="fadeTo">    </div>  <p>VeVb武林網(wǎng)是國內(nèi)專業(yè)的網(wǎng)站建設(shè)資源.</p>  </div> </div> <!--DEMO end-->  </body> </html> 

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



注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 台北市| 南乐县| 云林县| 集贤县| 彭州市| 宣化县| 陆丰市| 安仁县| 东莞市| 安乡县| 花莲市| 克拉玛依市| 伊宁县| 水富县| 遵化市| 九江市| 稷山县| 抚松县| 阜城县| 易门县| 荣成市| 平阳县| 额尔古纳市| 古丈县| 华蓥市| 华安县| 张掖市| 闵行区| 云梦县| 什邡市| 兰坪| 洛扎县| 东山县| 陵川县| 丹棱县| 阜康市| 广灵县| 广灵县| 积石山| 鄂托克旗| 双牌县|