整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

(原创)3种图片滚动js纯手写的写法

(原创)3种图片滚动js纯手写的写法

为一名职业web前端,我们需要对网页上常见的交互都要具备手写js的能力,或者js比较复杂如果习惯jquery也可以。最近切图网一个客户项目中遇到了图片滚动效果,因客户要求,写了3个不同的版本,留作备注和分享。

1,右箭头,点击一下移动一个单元,当移动到最后一个单元的时候,在点击右箭头,回到第一个单元

  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,点击右箭头,移动一个单元,当移动到最后一个单元的时候,点击右箭头无效。

    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设置动画方式负移位,制造图片左移的效果,然后设置移位为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,最常规的写法,参见切图框架 slicy 。

      http://www.slicy.cn

      原文地址:http://www.qietu.cn/thread-15196-1-1.html (切图社区)

      加微信公众号:qietuwang (限做前端的人)

景:

想要实现图片持续滚动,既然使用js,就千万不要加css动画、过渡等相关样式,如果想要滚动的平滑一下,可以一像素一像素的感动,则很平滑,如果加了过渡动画,当图片重置为0时,会有往回倒的动画效果,跟预期不符。

原理:

图片滚动原理同图片轮播原理,同样也适用于文字滚动等一系列滚动,通过复制最后一张图片或最后一堆文字插入第一行,或复制第一张图片或一堆文字插入在结尾,来实现无缝拼接,前提:1、必须是没有设置过渡动画的,2、重置为0的时候与当前已经滚动到的高度对于图片的位置而言肉眼看上去没变化。

实现:

html主要包含三块:

1、最外层盒子,用来展示滚动图的区域,overflow:hidden;

2、滚动的盒子,主要改变该盒子的定位值,来实现滚动,里面包含所有要滚动的图片或文字

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

代码:

class Roll {

constructor(opts) {

this.elem=opts.elem; // 图片包含滚动长度的元素的

this.elemBox=opts.elemBox; //图片展示区域元素,为了获取展示区域的高度

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:

// 默认向上滚动,如果滚动的盒子的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