亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js實(shí)現(xiàn)淘寶固定側(cè)邊欄

 更新時(shí)間:2022年07月03日 13:58:05   作者:阿旋要畢業(yè)~  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)淘寶固定側(cè)邊欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)淘寶固定側(cè)邊欄的具體代碼,供大家參考,具體內(nèi)容如下

1.實(shí)現(xiàn)效果:

當(dāng)頁(yè)面運(yùn)行到banner區(qū)域時(shí),右邊側(cè)邊欄改為固定定位,當(dāng)頁(yè)面運(yùn)行到主體區(qū)域時(shí),右邊側(cè)邊欄顯示返回到頂部。

2.思路:

(1)給document加scroll事件。

(2)獲取頁(yè)面被卷去的部分用window.pageYOffset.

(3)不斷判斷頁(yè)面滾動(dòng)了多少。計(jì)算右邊側(cè)邊欄應(yīng)該待的位置。

3.代碼:

pink老師用了固定定位fixed(固定定位是相對(duì)于窗口的距離),我做的還是用絕對(duì)定位(絕對(duì)定位是相對(duì)于父元素來說的,即document),都是可以實(shí)現(xiàn)的。

<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Document</title>
? ? <style>
? ? ? ? .top {
? ? ? ? ? ? width: 80%;
? ? ? ? ? ? height: 200px;
? ? ? ? ? ? background-color: pink;
? ? ? ? }
? ? ? ? .banner {
? ? ? ? ? ? width: 80%;
? ? ? ? ? ? height: 400px;
? ? ? ? ? ? background-color: aquamarine;
? ? ? ? }
? ? ? ? .main {
? ? ? ? ? ? width: 80%;
? ? ? ? ? ? height: 800px;
? ? ? ? ? ? background-color: red;
? ? ? ? }
? ? ? ? .foot {
? ? ? ? ? ? width: 80%;
? ? ? ? ? ? height: 400px;
? ? ? ? ? ? background-color:blanchedalmond;
? ? ? ? }
? ? ? ? .lan {
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? right:10%;
? ? ? ? ? ? top:400px;
? ? ? ? ? ? width: 80px;
? ? ? ? ? ? height: 80px;
? ? ? ? ? ? background-color: cadetblue;
? ? ? ? }
? ? </style>
</head>
<body>
? ? <div class="top">頭部區(qū)域</div>
? ? <div class="banner">banner區(qū)域</div>
? ? <div class="main">頭部區(qū)域</div>
? ? <div class="foot">尾部區(qū)域</div>
? ? <div class="lan"></div>
? ? <script>
? ? ? ? var lan = document.querySelector('.lan');
? ? ? ? document.addEventListener('scroll', function() {
? ? ? ? ? ? console.log('jkjkkj');
? ? ? ? ? ? var top = window.pageYOffset;
? ? ? ? ? ? if(top ?> 200) {
? ? ? ? ? ? ? ? // 改為固定定位。
? ? ? ? ? ? ? ? var topp = 400-200 + top;
? ? ? ? ? ? ? ? lan.style.top = topp+'px';
? ? ? ? ? ? ? ? if(top > 600) {
? ? ? ? ? ? ? ? ? ? lan.innerHTML = '返回頂部';
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? lan.innerHTML = '';
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? lan.style.top = 400+'px';
? ? ? ? ? ? ? ? lan.innerHTML = '';
? ? ? ? ? ? }
? ? ? ? })
? ? </script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論