批改狀態(tài):未批改
老師批語:
知識點:
監(jiān)聽瀏覽器滾動: $().scroll([[data],fn])
當(dāng)用戶滾動指定的元素時,會發(fā)生 scroll 事件。 scroll 事件適用于所有可滾動的元素和 window 對象(瀏覽器窗口)
獲取匹配元素相對滾動條頂部的偏移:$().scrollTop([val])
無參數(shù)描述: 獲取第一段相對滾動條頂部的偏移
參數(shù)val 描述: 設(shè)置相對滾動條頂部的偏移
獲取匹配元素相對滾動條左側(cè)的偏移:$().scrollLeft([val])
無參數(shù)描述: 獲取第一段相對滾動條左側(cè)的偏移
參數(shù)val 描述: 設(shè)置相對滾動條左側(cè)的偏移
案例:根據(jù)檢測滾動距離然后顯示搜索框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>1.22 獲取滾動值案例</title> <script src="https://lib.baomitu.com/jquery/3.3.1/jquery.min.js"></script> <style> *{margin: 0;padding: 0;} .content{width: 980px;margin: 0 auto;height: auto;overflow: hidden;} .head{line-height: 50px;height: auto;overflow: hidden;min-height: 800px;background: url('imgs/dm.jpg') center 50px no-repeat;} .nav{position: fixed;top: 0;left: 0;width: 100%;background: #fff;} .nav .logo{text-decoration: none;color: #676570;font-size: 30px;float: left;} .nav ul{float: right;} .nav li{float: left;list-style: none;} .nav li a{display: block;padding: 0 10px;text-decoration: none;color: #7042b5;} .nav li a:hover{color: #676570;} .search{text-align: center;background: rgba(112, 66, 187, 0.3);padding: 10px 0;display: none;} .search form{border-radius: 15px;background: #fff;width: 250px;margin: 0 auto;height: 30px;overflow: hidden;} .search input,.search button{border: none;height: 30px;line-height: 30px;padding: 0 10px;} .search input{float: left;outline: none;} .search button{float: right;background: #7042b5;color: #fff;text-align: center;outline: none;} .container .content{min-height: 500px;background: lightgray;} </style> </head> <body> <div class="head"> <div class="nav"> <div class="content"> <a href="" class="logo"><b>LOGO</b></a> <ul> <li><a href="">HOME</a></li> <li><a href="">NEWS</a></li> <li><a href="">ABOUT</a></li> <li><a href="">CONACT</a></li> </ul> </div> <div class="search"> <form action=""> <input type="text" placeholder="請輸入關(guān)鍵詞"><button>全網(wǎng)搜</button> </form> </div> </div> </div> <div class="container"> <div class="content"></div> </div> <script> $(function () { $(window).scroll(function(){ if($(window).scrollTop() > 50){ $('.nav .content').slideUp(); $('.nav .search').slideDown(); }else{ $('.nav .content').slideDown(); $('.nav .search').slideUp(); } }) }) </script> </body> </html>
效果圖前:
效果圖后:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號