JavaScript實現(xiàn)長圖滾動效果
更新時間:2021年04月29日 11:52:37 作者:知識進(jìn)腦的肖老千啊
這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)長圖滾動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JavaScript之長圖滾動的具體代碼,供大家參考,具體內(nèi)容如下
長圖的滾動會涉及定時器:
我們先來回顧下定時器:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>定時器回顧</title>
</head>
<body>
<button id="start">開啟</button>
<button id="stop">關(guān)閉</button>
<script type="text/javascript">
var start = document.getElementById("start");
var stop = document.getElementById("stop");
var num = 0,timer = null;
start.onclick = function (){
// 使用定時器的時候 先清除原有定時器 再開啟定時器 可以試著將下邊的clearInterval(timer);注釋掉然后多次點擊開啟按鈕對比效果
clearInterval(timer);
timer = setInterval(function (){
num++;
console.log(num);
},1000)
}
stop.onclick = function (){
clearInterval(timer);
}
</script>
</body>
</html>
溫習(xí)完定時器內(nèi)容后,來看長圖滾動的代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>長圖滾動效果</title>
<style>
*{
padding: 0;
margin: 0;
}
body{
background-color: #000;
}
#box{
width: 658px;
height: 400px;
border: 1px solid #ff6700;
margin: 100px auto;
overflow: hidden;
position: relative;
}
#box img{
position: absolute;
top: 0;
left: 0;
}
#box span{
position: absolute;
width: 100%;
height: 50%;
left: 0;
cursor: pointer;
}
#box #top{
top: 0;
}
#box #bottom{
bottom: 0;
}
</style>
</head>
<body>
<div id="box">
<img src="img/timer.jpeg" alt="">
<span id="top"></span>
<span id="bottom"></span>
</div>
<script type="text/javascript">
// 1.獲取事件源
var box = document.getElementById('box');
var pic = document.getElementsByTagName('img')[0];
var divTop = document.getElementById('top');
var divBottom = document.getElementById('bottom');
// 2.添加事件
var num = 0,timer = null;
divBottom.onmouseover = function () {
// 清除之前的加速效果
clearInterval(timer);
// 讓圖片向下滾動
timer = setInterval(function () {
num -= 10;
// 這個-3666是根據(jù)圖片自己調(diào)控的
if(num >= -3666){
pic.style.top = num + 'px';
}else{
clearInterval(timer);
}
},50);
}
divTop.onmouseover = function () {
clearInterval(timer);
// 讓圖片向上滾動
timer = setInterval(function () {
num += 10;
if(num <= 0){
pic.style.top = num + 'px';
}else{
clearInterval(timer);
}
},100);
}
// 鼠標(biāo)移開則停止?jié)L動
box.onmouseout = function () {
clearInterval(timer);
}
</script>
</body>
</html>
這里不放效果圖了,需要可以自己試試(記得找長圖)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用base64對圖片的二進(jìn)制進(jìn)行編碼并用ajax進(jìn)行顯示
這篇文章主要介紹了使用base64對圖片的二進(jìn)制進(jìn)行編碼并用ajax進(jìn)行顯示的相關(guān)資料,需要的朋友可以參考下2017-01-01
微信小程序教程系列之設(shè)置標(biāo)題欄和導(dǎo)航欄(7)
這篇文章主要為大家詳細(xì)介紹了微信小程序教程系列之標(biāo)題欄和導(dǎo)航欄的設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
javascript中的self和this用法小結(jié)
本篇文章主要是對javascript中的self和this用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
JavaScript 阻塞方式實現(xiàn)異步任務(wù)隊列
本文主要介紹了JavaScript 阻塞方式實現(xiàn)異步任務(wù)隊列,主要介紹了兩種方案,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
js實現(xiàn)input框文字動態(tài)變換顯示效果
這篇文章主要介紹了js實現(xiàn)input框文字動態(tài)變換顯示效果,涉及javascript隨機字符串與中文的動態(tài)切換顯示效果,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08

