整合营销服务商

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

免费咨询热线:

“图片滑动样式”修改HTML教程

友们,下午好!

都说一张美美的图能为文章增色三分!

那如果是一个交互的图片样式 + 几张美美图呢?这能为文章增色多少呢?

比如这种(样式ID:90298)

使用这种样式,即能有效的展示图片,还能缩小文章空间,而且还与读者存在互动交互,想不想知道这种样式怎么做出来呢?

上面两种样式都可以在样式中心输入ID搜索到。

但是,样式中心的原样式,都是四张图片滑动的,直接进行换图就可以使用了。

但如果要像三儿上面做的两个样式,一个是5张图,一个是9张图,就要进HTML进行修改了。

教程一(带图片说明的样式)

进入到“HTML”模式,找到<section .........> </section>这段代码,先选择Ctrl+C复制。

然后在此段代码结尾处敲回车键换行,再选择Ctrl+V粘贴。

粘贴几次,样式就会在原有四张的基础上多出几张,胖友们可以根据自己的需求进行多次粘贴。

教程二

进入到“HTML”模式,找到<img src=........./>这段代码,先选择Ctrl+C复制,然后在此段代码结尾处,再Ctrl+V粘贴。

同上个样式,粘贴几次,样式就会在原有四张的基础上多出几张,胖友们可以根据自己的需求进行多次粘贴。

为了样式的美感,还是有三点建议给大家。

1、图片请保持尺寸一致。否则会导致图片层次不齐。

2、尺寸请500x500以上。否则可能会使图片不清楚。

3、图片大小尽可能小点。否则浏览时加载会不流畅。

更多好玩样式,请进样式中心搜索“滚动

好了,本次教程就到这里~bye

景:

想要实现图片持续滚动,既然使用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