JS庫(kù)之Waypoints的用法詳解
一款用于捕獲各種滾動(dòng)事件的插件?Waypoints。同時(shí)Waypoints還支持固定元素和無(wú)限滾動(dòng)的功能,功力十分強(qiáng)大。
一、最簡(jiǎn)易的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>waypoints的最簡(jiǎn)單使用</title>
<!-- 定義css樣式 -->
<style>
*{
padding: 0;
margin: 0;
}
#example-basic{
height: 500px;
text-align: center;
}
</style>
<!-- 引入js文件 -->
<script src="js/jquery-3.0.0.min.js"></script>
<script src="js/jquery.waypoints.js"></script>
<script src="js/jquery-ui.min.js"></script>
<!-- 啟動(dòng)waypoints -->
<script>
$(function () {
$(‘#example-basic‘).waypoint(function() {
console.log("hi,example-basic,你的頂部碰到了瀏覽器窗口的頂部!");//測(cè)試打開(kāi)web調(diào)試器,看控制臺(tái)信息
});
});
//注:無(wú)論是鼠標(biāo)向上或向下只要該元素的頂部碰到瀏覽器的頂部都會(huì)觸發(fā)waypoints事件
</script>
</head>
<body>
<div style="background:#ccc;height:1800px;">one div</div>
<div id="example-basic">example-basic.</div>
<div style="background:#ccc;height:1800px;">one div</div>
</html>
二、能檢測(cè)鼠標(biāo)滾動(dòng)方向的基本應(yīng)用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>檢測(cè)鼠標(biāo)滾動(dòng)方向</title>
<style>
*{
padding: 0;
margin: 0;
}
#example-basic{
height: 500px;
text-align: center;
}
.in{
font-size: 36px;
color: #ff0;
background:red;
transition:all 0.5s;
}
</style>
<script src="js/jquery-3.0.0.min.js"></script>
<script src="js/jquery.waypoints.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script>
$(function () {
$(‘#example-basic‘).waypoint(
function(direction){
if(direction=="down"){
$(‘#example-basic‘).addClass("in");
$(‘#example-basic‘).html("你在向下滾動(dòng)!")
}else{
$(‘#example-basic‘).removeClass("in");
$(‘#example-basic‘).html("你在向上滾動(dòng)!")
}
},//第1個(gè)參數(shù)為waypoints事件響應(yīng)時(shí)所執(zhí)行的代碼,是一個(gè)匿名函數(shù)即可
{
offset:‘50%‘
}//第2個(gè)參數(shù)為偏移量,本例即該div到窗口高度一半時(shí)觸發(fā)
);
});
</script>
</head>
<body>
<div style="background:#ccc;height:1800px;">one div</div>
<div id="example-basic">example-basic.</div>
<div style="background:#ccc;height:1800px;">one div</div>
</html>
三、鼠標(biāo)滾動(dòng)加動(dòng)畫(huà)效果的應(yīng)用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>加下動(dòng)畫(huà)效果的</title>
<style>
*{
padding: 0;
margin: 0;
}
div{
background: #eee;
}
.banner{
width: 1100px;
margin: 0 auto;
}
.title{
height: 100px;
background: #9f9;
}
.lt{
position: relative;
height: 400px;
border:1px dotted #999;
}
.lt_left{
position: absolute;
width: 500px;
height: 300px;
left: -20%;
top: 0;
margin-left: -550px;
background: #f99;
}
.lt_right{
position: absolute;
width: 500px;
height: 300px;
left: 120%;
top: 0;
margin-left: 50px;
background: #99f;
}
</style>
<script src="js/jquery-3.0.0.min.js"></script>
<script src="js/jquery.waypoints.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script>
$(function () {
//獲取運(yùn)動(dòng)的盒子
var boxElemets = $(‘.boxaction‘);
$.each(boxElemets, function() {
$(this).attr(‘init‘, ‘false‘);
});
//判斷是否出現(xiàn)在瀏覽器界面里面!
function isScrolledIntoView(elem) {
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
if (elemTop + 50 < docViewBottom) {
return true
} else {
return false
}
}
//定義動(dòng)畫(huà)
function animateInit() {
$.each(boxElemets, function() {
if ($(this).attr(‘init‘) == ‘false‘ && isScrolledIntoView($(this))) { //沒(méi)有顯示過(guò)且剛出現(xiàn)在瀏覽器界面
$(this).attr(‘init‘, ‘true‘);
$(this).animate({
‘left‘: ‘50%‘
}, 1000, ‘easeOutCubic‘);
}
});
}
animateInit(); //先執(zhí)行一次animateInit
$(window).scroll(function() { //滑動(dòng)執(zhí)行animateInit
animateInit();
});
})
</script>
</head>
<body>
<div style="background:#ccc;height:1800px;text-align:center;">top div</div>
<div class="banner">
<div class="title">這是標(biāo)題</div>
<div class="lt">
<div class="lt_left boxaction">這是左邊盒子</div>
<div class="lt_right boxaction">這是右邊盒子</div>
</div>
</div>
</body>
</html>
總結(jié)
以上所述是小編給大家介紹的JS庫(kù)之Waypoints的用法詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
自定義事件解決重復(fù)請(qǐng)求BUG的問(wèn)題
下面小編就為大家?guī)?lái)一篇自定義事件解決重復(fù)請(qǐng)求BUG的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
詳解js對(duì)象中屬性的兩種類(lèi)型之?dāng)?shù)據(jù)屬性和訪問(wèn)器屬性
在理解vue底層響應(yīng)式原理時(shí),了解到,原來(lái)對(duì)象中的屬性,不單單從表面看起來(lái)那么簡(jiǎn)單是key:value形式,而是還有隱藏的內(nèi)部特性,其中對(duì)象內(nèi)的屬性分為兩種類(lèi)型的屬性:數(shù)據(jù)屬性和訪問(wèn)器屬性,本文將給大家詳細(xì)介紹一下數(shù)據(jù)屬性和訪問(wèn)器屬性,需要的朋友可以參考下2023-05-05
layui button 按鈕彈出提示窗口,確定才進(jìn)行的方法
今天小編就為大家分享一篇layui button 按鈕彈出提示窗口,確定才進(jìn)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
JS實(shí)現(xiàn)側(cè)懸浮浮動(dòng)實(shí)例代碼
這篇文章主要介紹了JS實(shí)現(xiàn)側(cè)懸浮浮動(dòng)實(shí)例代碼,有需要的朋友可以參考一下2013-11-11
同一個(gè)網(wǎng)頁(yè)中實(shí)現(xiàn)多個(gè)JavaScript特效的方法
這篇文章主要介紹了同一個(gè)網(wǎng)頁(yè)中實(shí)現(xiàn)多個(gè)JavaScript特效的方法,實(shí)例分析了多個(gè)特效的實(shí)現(xiàn)原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02

