首页 » Web前端 » php图片滚动技巧_javascript实现单张或多张图片持续无缝滚动

php图片滚动技巧_javascript实现单张或多张图片持续无缝滚动

访客 2024-11-01 0

扫一扫用手机浏览

文章目录 [+]

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

事理:

php图片滚动技巧_javascript实现单张或多张图片持续无缝滚动

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

php图片滚动技巧_javascript实现单张或多张图片持续无缝滚动
(图片来自网络侵删)

实现:

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

标签:

相关文章

网站SEO优化关键词布局的艺术与步骤

网站SEO优化已成为企业获取流量、提升品牌知名度的重要手段。关键词布局作为SEO优化的核心环节,其重要性不言而喻。本文将深入探讨关...

Web前端 2025-04-09 阅读1 评论0

网站编辑与SEO协同发展的密不可分关系

网站编辑和SEO(搜索引擎优化)这两个职业逐渐成为互联网行业的热门话题。许多人认为,网站编辑和SEO是两个独立的领域,但实际上,它...

Web前端 2025-04-09 阅读1 评论0