JS實(shí)現(xiàn)圖片的不間斷連續(xù)滾動的簡單實(shí)例
js替代marquee實(shí)現(xiàn)圖片無縫滾動
可能大家都碰到過,當(dāng)marquee中滾動的是圖片的時候,滾到終點(diǎn)的時候直接就跳回到起點(diǎn)了,而不像文字那樣可以無縫滾動,下面介紹的是通過js來實(shí)現(xiàn)圖片的無縫滾動。
先了解一下下面這幾個屬性:
innerHTML: 設(shè)置或獲取位于對象起始和結(jié)束標(biāo)簽內(nèi)的 HTML
scrollHeight: 獲取對象的滾動高度。
scrollLeft: 設(shè)置或獲取位于對象左邊界和窗口中目前可見內(nèi)容的最左端之間的距離
scrollTop: 設(shè)置或獲取位于對象最頂端和窗口中可見內(nèi)容的最頂端之間的距離
scrollWidth: 獲取對象的滾動寬度
offsetHeight: 獲取對象相對于版面或由父坐標(biāo) offsetParent 屬性指定的父坐標(biāo)的高度
offsetLeft: 獲取對象相對于版面或由 offsetParent 屬性指定的父坐標(biāo)的計(jì)算左側(cè)位置
offsetTop: 獲取對象相對于版面或由 offsetTop 屬性指定的父坐標(biāo)的計(jì)算頂端位置
offsetWidth: 獲取對象相對于版面或由父坐標(biāo) offsetParent 屬性指定的父坐標(biāo)的寬度
-----------------------------------------------------------------------
圖片向上無縫滾動
<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> 向上滾動 <div id="demo"> <div id="demo1"> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.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)滾動至demo1與demo2交界時 tab.scrollTop-=tab1.offsetHeight //demo跳到最頂端 else{ tab.scrollTop++ } } var MyMar=setInterval(Marquee,speed); tab.onmouseover=function() {clearInterval(MyMar)};//鼠標(biāo)移上時清除定時器達(dá)到滾動停止的目的 tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)};//鼠標(biāo)移開時重設(shè)定時器 --> </script>
圖片向下無縫滾動
<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> 向下滾動 <div id="demo"> <div id="demo1"> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.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)滾動至demo1與demo2交界時 tab.scrollTop+=tab2.offsetHeight //demo跳到最頂端 else{ tab.scrollTop-- } } var MyMar=setInterval(Marquee,speed); tab.onmouseover=function() {clearInterval(MyMar)};//鼠標(biāo)移上時清除定時器達(dá)到滾動停止的目的 tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)};//鼠標(biāo)移開時重設(shè)定時器 --> </script>
圖片向左無縫滾動
<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> 向左滾動 <div id="demo"> <div id="indemo"> <div id="demo1"> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.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>
圖片向右無縫滾動
<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> 向右滾動 <div id="demo"> <div id="indemo"> <div id="demo1"> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.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>
最后,如果有人想一個頁面有兩個滾動圖片集,一個往左一個往右,那下面的能用了。我把js都加個i了,還有css
向右滾動
<div id="demoi"> <div id="indemoi"> <div id="demoi1"> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.cn/other/link/Clear_logo.gif" border="0" /></a> <a href="#"><img src="http://chabaoo.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ù)滾動的簡單實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用遞增的數(shù)字返回循環(huán)漸變的顏色的js代碼
其實(shí)很久前就想寫一個這樣的函數(shù)了。因?yàn)楹芏鄷r候需要利用遞增數(shù)字返回一個漸變顏色序列,今天終于完成了。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ì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03詳解ant-design-pro使用qiankun微服務(wù)
這篇文章主要介紹了ant-design-pro使用qiankun微服務(wù)詳解,其實(shí)微服務(wù)需要有主應(yīng)用和子應(yīng)用,?一個子應(yīng)用可以配置多個相關(guān)聯(lián)的主應(yīng)用,配置方法都是一樣的,對ant-design-pro微服務(wù)配置相關(guān)知識,感興趣的朋友一起看看吧2022-03-03