最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

js实现图片无缝滚动

来源:动视网 责编:小采 时间:2020-11-03 12:31:14
文档

js实现图片无缝滚动

js实现图片无缝滚动:实现原理:图片滚动原理同图片轮播原理,同样也适用于文字滚动等一系列滚动,通过复制最后一张图片或最后一堆文字插入第一行,或复制第一张图片或一堆文字插入在结尾,来实现无缝拼接。前提:1、必须是没有设置过渡动画的,2、重置为0的时候与当前已经滚动到
推荐度:
导读js实现图片无缝滚动:实现原理:图片滚动原理同图片轮播原理,同样也适用于文字滚动等一系列滚动,通过复制最后一张图片或最后一堆文字插入第一行,或复制第一张图片或一堆文字插入在结尾,来实现无缝拼接。前提:1、必须是没有设置过渡动画的,2、重置为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)
 }
}

推荐教程:js入门教程

文档

js实现图片无缝滚动

js实现图片无缝滚动:实现原理:图片滚动原理同图片轮播原理,同样也适用于文字滚动等一系列滚动,通过复制最后一张图片或最后一堆文字插入第一行,或复制第一张图片或一堆文字插入在结尾,来实现无缝拼接。前提:1、必须是没有设置过渡动画的,2、重置为0的时候与当前已经滚动到
推荐度:
标签: 图片 滚动 js
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top