整合營銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          (原創(chuàng))3種圖片滾動js純手寫的寫法

          (原創(chuàng))3種圖片滾動js純手寫的寫法

          為一名職業(yè)web前端,我們需要對網(wǎng)頁上常見的交互都要具備手寫js的能力,或者js比較復(fù)雜如果習(xí)慣jquery也可以。最近切圖網(wǎng)一個客戶項目中遇到了圖片滾動效果,因客戶要求,寫了3個不同的版本,留作備注和分享。

          1,右箭頭,點擊一下移動一個單元,當(dāng)移動到最后一個單元的時候,在點擊右箭頭,回到第一個單元

          1. /*圖片滾動*/

          2. $('.imgroll').each(function(){

          3. $(this).find('li:first').addClass('selected');

          4. })

          5. $('.imgroll .next').click(function(){

          6. var f=$(this).parent();

          7. var l=(f.find('li').size()-4) * 258;

          8. //console.log(parseInt(f.find('ul').css('margin-left')) + '-' + l);

          9. if(parseInt(f.find('ul').css('margin-left')) > -l){

          10. f.find('ul').stop().animate({'marginLeft':'-=258'})

          11. }

          12. else{

          13. f.find('ul').stop().animate({'marginLeft':0})

          14. }

          15. });

          16. $('.imgroll .prev').click(function(){

          17. var f=$(this).parent();

          18. var l=(f.find('li').size()-4) * 258;

          19. //console.log(parseInt(f.find('ul').css('margin-left')) + '-' + l);

          20. if(parseInt(f.find('ul').css('margin-left')) < 0){

          21. f.find('ul').stop().animate({'marginLeft':'+=258'})

          22. }

          23. else{

          24. f.find('ul').stop().animate({'marginLeft':0})

          25. }

          26. })

          27. 2,點擊右箭頭,移動一個單元,當(dāng)移動到最后一個單元的時候,點擊右箭頭無效。

            1. /*圖片滾動*/

            2. $('.imgroll').each(function(){

            3. $(this).find('li').each(function(){

            4. $(this).attr('data-index',$(this).index());

            5. })

            6. })

            7. $('.imgroll .next').click(function(){

            8. // 將整個ul設(shè)置動畫方式負(fù)移位,制造圖片左移的效果,然后設(shè)置移位為0,將第一張圖片獲取補到最后,到這里整個圖片左移效果完成

            9. var f=$(this).parent();

            10. //console.log(f.find('li:eq(3)').data('index')+1 + '-----'+ f.find('li').size());

            11. if(f.find('li:eq(3)').data('index')+ 1==f.find('li').size()){

            12. return false;

            13. }

            14. f.find('ul').animate({'marginLeft':-258},function(){

            15. $(this).css('marginLeft',0).find('li:first').appendTo($(this));

            16. });

            17. });

            18. $('.imgroll .prev').click(function(){

            19. var f=$(this).parent();

            20. if(f.find('li:first').data('index')==0){

            21. return false;

            22. }

            23. // 同上

            24. f.find('ul').css('marginLeft',-258).find('li:last').prependTo(f.find('ul'));

            25. f.find('ul').animate({'marginLeft':0});

            26. })

            27. 3,最常規(guī)的寫法,參見切圖框架 slicy 。

              http://www.slicy.cn

              原文地址:http://www.qietu.cn/thread-15196-1-1.html (切圖社區(qū))

              加微信公眾號:qietuwang (限做前端的人)

          景:

          想要實現(xiàn)圖片持續(xù)滾動,既然使用js,就千萬不要加css動畫、過渡等相關(guān)樣式,如果想要滾動的平滑一下,可以一像素一像素的感動,則很平滑,如果加了過渡動畫,當(dāng)圖片重置為0時,會有往回倒的動畫效果,跟預(yù)期不符。

          原理:

          圖片滾動原理同圖片輪播原理,同樣也適用于文字滾動等一系列滾動,通過復(fù)制最后一張圖片或最后一堆文字插入第一行,或復(fù)制第一張圖片或一堆文字插入在結(jié)尾,來實現(xiàn)無縫拼接,前提:1、必須是沒有設(shè)置過渡動畫的,2、重置為0的時候與當(dāng)前已經(jīng)滾動到的高度對于圖片的位置而言肉眼看上去沒變化。

          實現(xiàn):

          html主要包含三塊:

          1、最外層盒子,用來展示滾動圖的區(qū)域,overflow:hidden;

          2、滾動的盒子,主要改變該盒子的定位值,來實現(xiàn)滾動,里面包含所有要滾動的圖片或文字

          3、包含圖片或文字的盒子。

          代碼:

          class Roll {

          constructor(opts) {

          this.elem=opts.elem; // 圖片包含滾動長度的元素的

          this.elemBox=opts.elemBox; //圖片展示區(qū)域元素,為了獲取展示區(qū)域的高度

          this.direction=opts.direction;

          this.time=opts.time;

          this.init();

          this.roll=this.roll.bind(this)

          this.startRoll=this.startRoll.bind(this)

          this.stopRoll=this.stopRoll.bind(this)

          }

          init(){

          this.elemHeight=this.elem.offsetHeight;

          this.elemHtml=this.elem.innerHTML;

          this.elem.innerHTML=this.elem.innerHTML + this.elemHtml+ this.elemHtml;

          this.speed;

          // 如果向上滾或者向左滾動每次減1,向下滾或者向右滾動每次加1

          if(this.direction==='top' || this.direction==='left'){

          this.speed=-1;

          }else{

          this.speed=1;

          }

          }

          roll(){

          switch (this.direction) {

          case "top":

          // 如果滾動的盒子的top值超出元素的高度,則置為0

          if(Math.abs(this.elemBox.offsetTop) >=this.elemHeight){

          this.elemBox.style.top=0;

          }else{

          this.elemBox.style.top=this.elemBox.offsetTop + this.speed + 'px';

          }

          break;

          case "bottom":

          // 如果滾動的盒子的bottom值超出元素的高度,則置為0

          if(Math.abs(this.elemBox.offsetBottom) >=this.elemHeight){

          this.elemBox.style.bottom=0;

          }else{

          this.elemBox.style.bottom=this.elemBox.offsetBottom + this.speed + 'px';

          }

          break;

          case "left":

          // 如果滾動的盒子的left超出元素的高度,則置為0

          if(Math.abs(this.elemBox.offsetLeft) >=this.elemHeight){

          this.elemBox.style.left=0;

          }else{

          this.elemBox.style.left=this.elemBox.offsetLeft + this.speed + 'px';

          }

          break;

          case "right":

          // 如果滾動的盒子的right超出元素的高度,則置為0

          if(Math.abs(this.elemBox.offsetRight) >=this.elemHeight){

          this.elemBox.style.right=0;

          }else{

          this.elemBox.style.right=this.elemBox.offsetRight + this.speed + 'px';

          }

          break;

          default:

          // 默認(rèn)向上滾動,如果滾動的盒子的top超出元素的高度,則置為0

          if(Math.abs(this.elemBox.offsetTop) >=this.elemHeight){

          this.elemBox.style.top=0;

          }else{

          this.elemBox.style.top=this.elemBox.offsetTop + speed + 'px';

          }

          }

          }

          stopRoll(){

          clearInterval(this.scrollTimer)

          }

          startRoll(){

          this.scrollTimer=setInterval(this.roll,this.time)

          }

          }



          原文鏈接:https://www.php.cn/js-tutorial-448891.html


          主站蜘蛛池模板: 亚洲AⅤ视频一区二区三区| 国产成人精品一区二区三区无码| 亚洲一区二区视频在线观看| 波多野结衣一区二区免费视频 | 国产乱码精品一区二区三区香蕉 | 国产中文字幕一区| 日本亚洲国产一区二区三区| 无码中文人妻在线一区二区三区| 国产亚洲福利精品一区二区| 日韩欧国产精品一区综合无码| 成人免费一区二区三区在线观看| 日韩免费观看一区| 日韩一区二区三区在线精品| 日韩av无码一区二区三区| 国产激情一区二区三区在线观看| 久久青草精品一区二区三区| 精品乱子伦一区二区三区高清免费播放 | 国产内射999视频一区| 一区二区三区电影网| 国产精品一区二区毛卡片| 色窝窝无码一区二区三区| 怡红院一区二区三区| 在线日产精品一区| 日本无码一区二区三区白峰美 | 色狠狠一区二区三区香蕉| 亚洲成av人片一区二区三区| 韩国精品一区二区三区无码视频| 精品不卡一区二区| 无码少妇精品一区二区免费动态| 精品中文字幕一区在线| 波多野结衣电影区一区二区三区| 国产成人av一区二区三区在线观看| 成人一区二区三区视频在线观看| AV天堂午夜精品一区二区三区| 亚洲综合激情五月色一区| 精品亚洲av无码一区二区柚蜜| 成人免费视频一区| 色婷婷AV一区二区三区浪潮| 国产一区二区视频在线观看| 精品一区二区三区免费毛片爱 | 亚洲一区二区三区在线播放 |