JS實(shí)現(xiàn)圖片的不間斷連續(xù)滾動(dòng)的簡單實(shí)例
js替代marquee實(shí)現(xiàn)圖片無縫滾動(dòng)
可能大家都碰到過,當(dāng)marquee中滾動(dòng)的是圖片的時(shí)候,滾到終點(diǎn)的時(shí)候直接就跳回到起點(diǎn)了,而不像文字那樣可以無縫滾動(dòng),下面介紹的是通過js來實(shí)現(xiàn)圖片的無縫滾動(dòng)。
先了解一下下面這幾個(gè)屬性:
innerHTML: 設(shè)置或獲取位于對(duì)象起始和結(jié)束標(biāo)簽內(nèi)的 HTML
scrollHeight: 獲取對(duì)象的滾動(dòng)高度。
scrollLeft: 設(shè)置或獲取位于對(duì)象左邊界和窗口中目前可見內(nèi)容的最左端之間的距離
scrollTop: 設(shè)置或獲取位于對(duì)象最頂端和窗口中可見內(nèi)容的最頂端之間的距離
scrollWidth: 獲取對(duì)象的滾動(dòng)寬度
offsetHeight: 獲取對(duì)象相對(duì)于版面或由父坐標(biāo) offsetParent 屬性指定的父坐標(biāo)的高度
offsetLeft: 獲取對(duì)象相對(duì)于版面或由 offsetParent 屬性指定的父坐標(biāo)的計(jì)算左側(cè)位置
offsetTop: 獲取對(duì)象相對(duì)于版面或由 offsetTop 屬性指定的父坐標(biāo)的計(jì)算頂端位置
offsetWidth: 獲取對(duì)象相對(duì)于版面或由父坐標(biāo) offsetParent 屬性指定的父坐標(biāo)的寬度
-----------------------------------------------------------------------
圖片向上無縫滾動(dòng)
<style type="text/css"> <!-- #demo { background: #FFF; overflow:hidden; border: 1px dashed #CCC; height: 100px; text-align: center; float: left; } #demo img { border: 3px solid #F2F2F2; display: block; } --> </style> 向上滾動(dòng) <div id="demo"> <div id="demo1"> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> </div> <div id="demo2"></div> </div> <script> <!-- var speed=10; //數(shù)字越大速度越慢 var tab=document.getElementByIdx_x("demo"); var tab1=document.getElementByIdx_x("demo1"); var tab2=document.getElementByIdx_x("demo2"); tab2.innerHTML=tab1.innerHTML; //克隆demo1為demo2 function Marquee(){ if(tab2.offsetTop-tab.scrollTop<=0)//當(dāng)滾動(dòng)至demo1與demo2交界時(shí) tab.scrollTop-=tab1.offsetHeight //demo跳到最頂端 else{ tab.scrollTop++ } } var MyMar=setInterval(Marquee,speed); tab.onmouseover=function() {clearInterval(MyMar)};//鼠標(biāo)移上時(shí)清除定時(shí)器達(dá)到滾動(dòng)停止的目的 tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)};//鼠標(biāo)移開時(shí)重設(shè)定時(shí)器 --> </script>
圖片向下無縫滾動(dòng)
<style type="text/css"> <!-- #demo { background: #FFF; overflow:hidden; border: 1px dashed #CCC; height: 100px; text-align: center; float: left; } #demo img { border: 3px solid #F2F2F2; display: block; } --> </style> 向下滾動(dòng) <div id="demo"> <div id="demo1"> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> </div> <div id="demo2"></div> </div> <script> <!-- var speed=10; //數(shù)字越大速度越慢 var tab=document.getElementByIdx_x("demo"); var tab1=document.getElementByIdx_x("demo1"); var tab2=document.getElementByIdx_x("demo2"); tab2.innerHTML=tab1.innerHTML; //克隆demo1為demo2 tab.scrollTop=tab.scrollHeight function Marquee(){ if(tab1.offsetTop-tab.scrollTop>=0)//當(dāng)滾動(dòng)至demo1與demo2交界時(shí) tab.scrollTop+=tab2.offsetHeight //demo跳到最頂端 else{ tab.scrollTop-- } } var MyMar=setInterval(Marquee,speed); tab.onmouseover=function() {clearInterval(MyMar)};//鼠標(biāo)移上時(shí)清除定時(shí)器達(dá)到滾動(dòng)停止的目的 tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)};//鼠標(biāo)移開時(shí)重設(shè)定時(shí)器 --> </script>
圖片向左無縫滾動(dòng)
<style type="text/css"> <!-- #demo { background: #FFF; overflow:hidden; border: 1px dashed #CCC; width: 500px; } #demo img { border: 3px solid #F2F2F2; } #indemo { float: left; width: 800%; } #demo1 { float: left; } #demo2 { float: left; } --> </style> 向左滾動(dòng) <div id="demo"> <div id="indemo"> <div id="demo1"> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> </div> <div id="demo2"></div> </div> </div> <script> <!-- var speed=10; //數(shù)字越大速度越慢 var tab=document.getElementByIdx_x("demo"); var tab1=document.getElementByIdx_x("demo1"); var tab2=document.getElementByIdx_x("demo2"); tab2.innerHTML=tab1.innerHTML; function Marquee(){ if(tab2.offsetWidth-tab.scrollLeft<=0) tab.scrollLeft-=tab1.offsetWidth else{ tab.scrollLeft++; } } var MyMar=setInterval(Marquee,speed); tab.onmouseover=function() {clearInterval(MyMar)}; tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)}; --> </script>
圖片向右無縫滾動(dòng)
<style type="text/css"> <!-- #demo { background: #FFF; overflow:hidden; border: 1px dashed #CCC; width: 500px; } #demo img { border: 3px solid #F2F2F2; } #indemo { float: left; width: 800%; } #demo1 { float: left; } #demo2 { float: left; } --> </style> 向右滾動(dòng) <div id="demo"> <div id="indemo"> <div id="demo1"> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> </div> <div id="demo2"></div> </div> </div> <script> <!-- var speed=10; //數(shù)字越大速度越慢 var tab=document.getElementByIdx_x("demo"); var tab1=document.getElementByIdx_x("demo1"); var tab2=document.getElementByIdx_x("demo2"); tab2.innerHTML=tab1.innerHTML; function Marquee(){ if(tab.scrollLeft<=0) tab.scrollLeft+=tab2.offsetWidth else{ tab.scrollLeft--; } } var MyMar=setInterval(Marquee,speed); tab.onmouseover=function() {clearInterval(MyMar)}; tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)}; --> </script>
最后,如果有人想一個(gè)頁面有兩個(gè)滾動(dòng)圖片集,一個(gè)往左一個(gè)往右,那下面的能用了。我把js都加個(gè)i了,還有css
向右滾動(dòng)
<div id="demoi"> <div id="indemoi"> <div id="demoi1"> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://www.dbjr.com.cn/other/link/Clear_logo.gif" border="0" /></a> </div> <div id="demoi2"></div> </div> </div> <script> <!-- var speedi=10; //數(shù)字越大速度越慢 var tabi=document.getElementByIdx_x("demoi"); var tabi1=document.getElementByIdx_x("demoi1"); var tabi2=document.getElementByIdx_x("demoi2"); tabi2.innerHTML=tabi1.innerHTML; function Marqueei(){ if(tabi.scrollLeft<=0) tabi.scrollLeft+=tabi2.offsetWidth else{ tabi.scrollLeft--; } } var MyMari=setInterval(Marqueei,speedi); tabi.onmouseover=function() {clearInterval(MyMari)}; tabi.onmouseout=function() {MyMari=setInterval(Marqueei,speedi)}; --> </script>
以上這篇JS實(shí)現(xiàn)圖片的不間斷連續(xù)滾動(dòng)的簡單實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript html5 canvas繪制時(shí)鐘效果
這篇文章主要介紹了JavaScript html5繪制時(shí)鐘效果的相關(guān)資料,使用HTML5的canvas標(biāo)簽和Javascript腳本,模擬顯示了一個(gè)時(shí)鐘,感興趣的小伙伴們可以參考一下2016-03-03利用遞增的數(shù)字返回循環(huán)漸變的顏色的js代碼
其實(shí)很久前就想寫一個(gè)這樣的函數(shù)了。因?yàn)楹芏鄷r(shí)候需要利用遞增數(shù)字返回一個(gè)漸變顏色序列,今天終于完成了。2008-10-10在vscode上直接運(yùn)行typescript的操作方法
在學(xué)習(xí)typescript的過程中發(fā)現(xiàn)在vscode上不能很好地的輸出typescript的運(yùn)行結(jié)果,需要先將typescript編譯為javascript,在通過node執(zhí)行js文件得到結(jié)果,這篇文章給大家介紹如何在vscode上直接運(yùn)行typescript,感興趣的朋友一起看看吧2023-12-12JavaScript實(shí)現(xiàn)隨機(jī)點(diǎn)名器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)隨機(jī)點(diǎn)名器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03詳解ant-design-pro使用qiankun微服務(wù)
這篇文章主要介紹了ant-design-pro使用qiankun微服務(wù)詳解,其實(shí)微服務(wù)需要有主應(yīng)用和子應(yīng)用,?一個(gè)子應(yīng)用可以配置多個(gè)相關(guān)聯(lián)的主應(yīng)用,配置方法都是一樣的,對(duì)ant-design-pro微服務(wù)配置相關(guān)知識(shí),感興趣的朋友一起看看吧2022-03-03