JS實現(xiàn)圖片的不間斷連續(xù)滾動的簡單實例
js替代marquee實現(xiàn)圖片無縫滾動
可能大家都碰到過,當(dāng)marquee中滾動的是圖片的時候,滾到終點的時候直接就跳回到起點了,而不像文字那樣可以無縫滾動,下面介紹的是通過js來實現(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)的計算左側(cè)位置
offsetTop: 獲取對象相對于版面或由 offsetTop 屬性指定的父坐標(biāo)的計算頂端位置
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://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/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://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/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://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/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://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/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://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/other/link/Clear_logo.gif" border="0" /></a>
<a href="#"><img src="http://www.dhdzp.com/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實現(xiàn)圖片的不間斷連續(xù)滾動的簡單實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
electron-builder允許安裝時請求提升權(quán)限的場景分析
electron-builder 作為一個用于 Electron 應(yīng)用程序打包的工具,需要下載并使用 Electron 運行時來創(chuàng)建可執(zhí)行文件,這篇文章給大家介紹electron-builder允許安裝時請求提升權(quán)限的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧2024-03-03
微信小程序?qū)崿F(xiàn)活動報名登記功能(實例代碼)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)活動報名登記,本篇將介紹使用微信小程序?qū)崿F(xiàn)發(fā)起一個活動報名的設(shè)計,以此為基礎(chǔ),我們可以掌握微信小程序表單的基本用法,進(jìn)而在諸如疫情信息登記、出入報備等場景中使用小程序進(jìn)行開發(fā),滿足相關(guān)的需求,需要的朋友可以參考下2022-09-09
Wordpress ThickBox 點擊圖片顯示下一張圖的修改方法
Wordpress自帶的ThickBox特效中,單擊圖片會調(diào)用 tb_remove 以關(guān)閉特效窗口,現(xiàn)將修改其動作為顯示下一張圖。2010-12-12
js控制iframe的高度/寬度讓其自適應(yīng)內(nèi)容
這篇文章主要介紹了如何使用js控制iframe的高度/寬度讓其自適應(yīng)內(nèi)容,需要的朋友可以參考下2014-04-04
用js實現(xiàn)下載遠(yuǎn)程文件并保存在本地的腳本
//將常用的vbs下載者改成js版了。本來想用jsc.exe編譯,可是不成功。jsc.exe不認(rèn)WScript2008-05-05
詳解JavaScript中Proxy與Object.defineProperty的區(qū)別
Proxy和Object.defineProperty都是JavaScript中用于實現(xiàn)對象屬性攔截和代理的機制,但它們在功能和應(yīng)用方面有一些區(qū)別,本文通過代碼示例詳細(xì)介紹了二者的區(qū)別,感興趣的朋友可以參考下2023-06-06
JavaScript動態(tài)創(chuàng)建div等元素實例講解
這篇文章主要介紹了JavaScript動態(tài)創(chuàng)建div等元素實例,2016-01-01

