天要實現(xiàn)的是字體上下無縫滾動效果,在友情鏈接這塊,2345提供了很多的鏈接并且通過定時的無縫滾動讓內(nèi)容顯示在窗口中,這里我也來寫下這個功能,當頁面載入時鏈接自動往上無縫滾動,當鼠標停留時滾動停止。
首先我們先來實現(xiàn)字幕滾動效果,一般情況下如果是多個單條數(shù)據(jù)翻滾,比較單間的方法就是用css來實現(xiàn),通過animation和@keyframes配合就可以快速的實現(xiàn)滾動,但是但是這時會有個問題,滾到最后一條時會自動跳到第一條,中間出現(xiàn)了斷層的感覺,所以使用該方法時最好將第一條數(shù)據(jù)復(fù)制一遍放尾部,然后通過animation-fill-mode: forwards;將動畫重置為第一幀,這樣就能夠?qū)崿F(xiàn)無縫的滾動了。
.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); } }
這里我的數(shù)據(jù)是通過p標簽遍歷a標簽得到的,所以每行有好多個a標簽,具體個數(shù)與內(nèi)容長短有關(guān),所以復(fù)制第一條無法實現(xiàn),所以這方法不合適我這里使用,所以這里我們還是用js來實現(xiàn)吧,為了更好的獲取元素的位置,這里稍微改動下HTML,在列表下面加個p標簽方便定位。
接下來我們開始寫方法,在methods中寫個paly方法, 通過document.getElementById來獲取當前元素,這里scrollTop獲取被選元素的垂直滾動條位置,offsetHeight獲取該控件本身的高度,然后設(shè)置一個定時器,給定一個speed時間,這樣就實現(xiàn)了自動無縫滾動的效果了。
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()中調(diào)用paly方法即可,還有.linkContent比忘了加上overflow: hidden;屬性。
mounted(){ this.play(); },
這是結(jié)合了JavaScript語法實現(xiàn)的文字無縫滾動效果,雖然達到了預(yù)期,但是現(xiàn)在在用vue,我還是想用vue來實現(xiàn)這個功能,只是目前還沒有研究出來,主要是因為我的a標簽數(shù)據(jù)是全包裹在一個p標簽中的,也就是每行的數(shù)據(jù)不定,內(nèi)容也不定,所以只能通過移動p標簽外面的div來實現(xiàn)功能,不知有沒有大神能指點一二。
當父div的行高等于自身高度時,內(nèi)部的行內(nèi)元素會上下居中顯示。行內(nèi)塊沒有固定高度時也會上下居中顯示。所以需要對父div的 line-height 進行調(diào)整。利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長度為父div的高度(寬度)的100%。定位屬性top和bottom(或是left和right)值分別設(shè)置為0,但子div有固定高度(寬度),并不能達到上下(左右)間距為0,此時給子div設(shè)置 margin:auto 會使它居中顯示。
轉(zhuǎn)載自喜歡JS的無名小站
例如 一個父div(w:100%;h:400px)中有一個子div(w:100px;100px;)。讓其上下左右居中。
利用表格單元格的居中屬性。
父div外層配置一個div,同時設(shè)置為表格元素 (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%;
當父div的行高等于自身高度時,內(nèi)部的行內(nèi)元素會上下居中顯示。行內(nèi)塊沒有固定高度時也會上下居中顯示。通過文本居中屬性text-align:center
,可以使內(nèi)部行內(nèi)元素或行內(nèi)塊元素左右居中顯示。
子div設(shè)定為行內(nèi)塊元素(display:inline-block);
父div設(shè)置行高(line-height)使子div上下居中
父div設(shè)置文本居中(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>
注: 行高如果設(shè)置為當前父div的高度(400px)的話,有固定高度的子div并不會居中顯示的,問題出在瀏覽器默認將其當做文本居中的,即把它當做了一段文本(chrome默認font-size:16px;hight:21px)進行居中,沒把它當做高度100px進行居中。所以需要對父div的line-height
進行調(diào)整。以font-size:0
(對應(yīng)的字體高度為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)值分別設(shè)置為0,但子div有固定高度(寬度),并不能達到上下(左右)間距為0,此時給子div設(shè)置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>
彈性盒子,自帶的一個居中功能
<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實現(xiàn)垂直居中的5種方法--前端觀察
們經(jīng)常看見某些元素會上上下下,比如箭頭的指引效果,這個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>
--------------------------------
來源:切圖網(wǎng)(qietu.com)始于2007年,專注web前端開發(fā)、培訓。
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。