整合营销服务商

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

免费咨询热线:

vue实战开发010:JS实现字体上下无缝滚动效果

天要实现的是字体上下无缝滚动效果,在友情链接这块,2345提供了很多的链接并且通过定时的无缝滚动让内容显示在窗口中,这里我也来写下这个功能,当页面载入时链接自动往上无缝滚动,当鼠标停留时滚动停止。

首先我们先来实现字幕滚动效果,一般情况下如果是多个单条数据翻滚,比较单间的方法就是用css来实现,通过animation和@keyframes配合就可以快速的实现滚动,但是但是这时会有个问题,滚到最后一条时会自动跳到第一条,中间出现了断层的感觉,所以使用该方法时最好将第一条数据复制一遍放尾部,然后通过animation-fill-mode: forwards;将动画重置为第一帧,这样就能够实现无缝的滚动了。

.linkContent {
 width: 90%;
 height: 20px;
 animation: move 3s infinite 2s running; 
 animation-fill-mode: forwards; 
}
@keyframes move {
 0% {
 transform:translatey(0px);
 }
 100% {
 transform:translateY(-20px);
 }
}

这里我的数据是通过p标签遍历a标签得到的,所以每行有好多个a标签,具体个数与内容长短有关,所以复制第一条无法实现,所以这方法不合适我这里使用,所以这里我们还是用js来实现吧,为了更好的获取元素的位置,这里稍微改动下HTML,在列表下面加个p标签方便定位。

接下来我们开始写方法,在methods中写个paly方法, 通过document.getElementById来获取当前元素,这里scrollTop获取被选元素的垂直滚动条位置,offsetHeight获取该控件本身的高度,然后设置一个定时器,给定一个speed时间,这样就实现了自动无缝滚动的效果了。

play () {
 var speed = 100;
 var wrapper = document.getElementById('wrapper');
 var demo1 = document.getElementById('demo1');
 var demo2 = document.getElementById('demo2');
 demo2.innerHTML=demo1.innerHTML 
 console.log(demo2.innerHTML)
 function Marquee(){ 
 if(demo2.offsetHeight-wrapper.scrollTop<=0) 
 wrapper.scrollTop-=demo1.offsetHeight; 
 else{ 
 wrapper.scrollTop+=1 
 } 
 } 
 var MyMar=setInterval(Marquee,speed) 
 wrapper.onmouseover=function() {clearInterval(MyMar)} 
 wrapper.onmouseout=function() {MyMar=setInterval(Marquee,speed)} 
}, 

因为 是获取当前的DOM元素进行操作的,所以这里我们要等页面载入之后再进行滚动操作,所以这里我们在mounted()中调用paly方法即可,还有.linkContent比忘了加上overflow: hidden;属性。

 mounted(){
 this.play();
 },

这是结合了JavaScript语法实现的文字无缝滚动效果,虽然达到了预期,但是现在在用vue,我还是想用vue来实现这个功能,只是目前还没有研究出来,主要是因为我的a标签数据是全包裹在一个p标签中的,也就是每行的数据不定,内容也不定,所以只能通过移动p标签外面的div来实现功能,不知有没有大神能指点一二。

容导读

当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。所以需要对父div的 line-height 进行调整。利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置 margin:auto 会使它居中显示。

转载自喜欢JS的无名小站

例如 一个父div(w:100%;h:400px)中有一个子div(w:100px;100px;)。让其上下左右居中。

方法一(varticle-align

理念

利用表格单元格的居中属性。

步骤

  • 父div外层配置一个div,同时设置为表格元素 (display: table),宽度为100%

  • 父div配置为表格单元格元素 (display: table-cell)

  • 父div配置居中属性(vertical-align: middle),使子div上下居中

  • 子div通过margin配置左右居中(margin-left:auto; margin-right:auto

例子

<style>
 * {margin: 0; padding: 0; box-sizing: border-box;}
 .table {display: table; width: 100%;}
 .father {display: table-cell; vertical-align: middle;}
 .son {margin: auto;}
</style>
<body>
 <div class="table" >
 <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
 <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
 </div>
 </div>
</body>

注:

  • 表格单元格比较特殊,如果只有一个单元格时,它的宽度默认会占父级(table|tr)宽度的100%;

  • table默认宽度不会撑开,需要手动配置width:100%;

方法二(line-height)

理念

当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。通过文本居中属性text-align:center,可以使内部行内元素或行内块元素左右居中显示。

步骤

  • 子div设定为行内块元素(display:inline-block);

  • 父div设置行高(line-height)使子div上下居中

  • 父div设置文本居中(text-align:center)使子div左右居中。

<style>
 * {margin: 0; padding: 0; box-sizing: border-box;}
 .father {line-height: 500px; text-align: center; font-size: 0;}
 .son { display: inline-block;
 /* display: inline-flex;
 display: inline-grid;
 display: inline-table; */
 }
</style>
<body>
 <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
 <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
 </div>
</body>

注: 行高如果设置为当前父div的高度(400px)的话,有固定高度的子div并不会居中显示的,问题出在浏览器默认将其当做文本居中的,即把它当做了一段文本(chrome默认font-size:16px;hight:21px)进行居中,没把它当做高度100px进行居中。所以需要对父div的line-height进行调整。以font-size:0(对应的字体高度为0)为例子,则需要line-height增加一个子div的高度(400px + 100px;)。

方法三(定位

理念

利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。

步骤

  • 父div标记下定位(position:relative|absolute|fixed);子div绝对定位(position:absolute

  • 子div上下居中:top:50%;margin-top:-h/2; 或是 bottom:50%;margin-bottom:-h/2;

  • 子div左右居中: left:50%;margin-left:-w/2 或是 right:50%;margin-right:-w/2

例子

<style>
 * {margin: 0; padding: 0; box-sizing: border-box;}
 .father {position: relative;}
 .son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px;
 }
</style>
<body>
 <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
 <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
 </div>
</body>

方法四(定位)

理念:

定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置margin:auto会使它居中显示。

步骤:

  • 父div标记下定位(position:relative|absolute|fixed|sticky);子div绝对定位(position:absolute

  • 子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto

  • 子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto

例子

<style>
 * {margin: 0; padding: 0; box-sizing: border-box;}
 .father {position: relative;}
 .son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto}
</style>
<body>
 <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
 <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
 </div>
</body>

方法五(flex)

理念

弹性盒子,自带的一个居中功能

例子

<style>
 * {margin: 0; padding: 0; box-sizing: border-box;}
 .father {display: flex; align-items: center}
 .son {margin: auto}
</style>
<body>
 <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
 <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
 </div>
</body>

flex兼容性,以及存在的已知问题

结尾

方法二和方法三兼容性要比其它好些

参考资料

Can I use
css-vertical-center-solution
CSS实现垂直居中的5种方法--前端观察

们经常看见某些元素会上上下下,比如箭头的指引效果,这个CSS3就可以完成此动作,简单好用,亲测。

@-webkit-keyframes bounce-down {

25% {

-webkit-transform: translateY(-10px);

}

50%, 100% {

-webkit-transform: translateY(0);

}

75% {

-webkit-transform: translateY(10px);

}

}

@keyframes bounce-down {

25% {

transform: translateY(-10px);

}

50%, 100% {

transform: translateY(0);

}

75% {

transform: translateY(10px);

}

}

.animate-bounce-down{

-webkit-animation: bounce-down 1.5s linear infinite;

animation: bounce-down 1.5s linear infinite;

}

@-webkit-keyframes bounce-up {

25% {

-webkit-transform: translateY(10px);

}

50%, 100% {

-webkit-transform: translateY(0);

}

75% {

-webkit-transform: translateY(-10px);

}

}

@keyframes bounce-up {

25% {

transform: translateY(10px);

}

50%, 100% {

transform: translateY(0);

}

75% {

transform: translateY(-10px);

}

}

.animate-bounce-up{

-webkit-animation: bounce-up 1.4s linear infinite;

animation: bounce-up 1.4s linear infinite;

}

运用

<div class="animate-bounce-up"></div>

--------------------------------

来源:切图网(qietu.com)始于2007年,专注web前端开发、培训。