js實(shí)現(xiàn)淘寶固定側(cè)邊欄
本文實(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í)有所幫助,也希望大家多多支持腳本之家。
- 博客側(cè)邊欄模塊跟隨滾動(dòng)條滑動(dòng)固定效果的實(shí)現(xiàn)方法(js+jquery等)
- javascript實(shí)現(xiàn)動(dòng)態(tài)側(cè)邊欄代碼
- JavaScript實(shí)現(xiàn)簡(jiǎn)單的隱藏式側(cè)邊欄功能示例
- javascript 實(shí)現(xiàn)動(dòng)態(tài)側(cè)邊欄實(shí)例詳解
- 利用js編寫響應(yīng)式側(cè)邊欄
- JS實(shí)現(xiàn)側(cè)邊欄鼠標(biāo)經(jīng)過彈出框+緩沖效果
- 基于slideout.js實(shí)現(xiàn)移動(dòng)端側(cè)邊欄滑動(dòng)特效
- JS運(yùn)動(dòng)框架之分享側(cè)邊欄動(dòng)畫實(shí)例
- JavaScript中實(shí)現(xiàn)無縫滾動(dòng)、分享到側(cè)邊欄實(shí)例代碼
- JS實(shí)現(xiàn)京東商品分類側(cè)邊欄
相關(guān)文章
解決微信瀏覽器Javascript無法使用window.location.reload()刷新頁(yè)面
這篇文章主要介紹在微信瀏覽器Javascript無法使用window.location.reload()刷新頁(yè)面的解決方法,比較實(shí)用,需要的朋友可以參考下。2016-06-06JavaScript中的ArrayBuffer詳細(xì)介紹
這篇文章主要介紹了JavaScript中的ArrayBuffer詳細(xì)介紹,本文講解了Array 在內(nèi)存中的堆棧模型、原始緩沖區(qū)的創(chuàng)建、數(shù)據(jù)化數(shù)組、DataView對(duì)象、XHR2 中的 ArrayBuffer等內(nèi)容,需要的朋友可以參考下2014-12-12js實(shí)現(xiàn)ajax的用戶簡(jiǎn)單登入功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)ajax的用戶簡(jiǎn)單登入功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06javascript實(shí)現(xiàn)俄羅斯方塊游戲的思路和方法
至于俄羅斯方塊的話,有很多的難點(diǎn),如果有JS去寫的話,要考慮到碰撞啊,邊界啊,下落等問題,本文這些問題大部分都有考慮到,這里給大家提供一部分思路,拋磚引玉,有需要的小伙伴可以參考下。2015-04-04原生JavaScript實(shí)現(xiàn)精美的淘寶輪播圖效果示例【附demo源碼下載】
這篇文章主要介紹了原生JavaScript實(shí)現(xiàn)精美的淘寶輪播圖效果,結(jié)合完整實(shí)例形式詳細(xì)分析了javascript實(shí)現(xiàn)淘寶輪播圖功能的相關(guān)HTML布局、css及js核心功能代碼,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-05-05BootStrap實(shí)現(xiàn)響應(yīng)式布局導(dǎo)航欄折疊隱藏效果(在小屏幕、手機(jī)屏幕瀏覽時(shí)自動(dòng)折疊隱藏)
這篇文章主要介紹了BootStrap實(shí)現(xiàn)導(dǎo)航欄的響應(yīng)式布局,當(dāng)在小屏幕、手機(jī)屏幕瀏覽時(shí)自動(dòng)折疊隱藏的效果,非常不錯(cuò),具有參考借鑒價(jià)值,對(duì)bootstrap 響應(yīng)式布局導(dǎo)航欄功能感興趣的朋友一起學(xué)習(xí)吧2016-11-11ionic2 tabs 圖標(biāo)自定義實(shí)例
這篇文章主要介紹了ionic2 tabs 圖標(biāo)自定義,需要的朋友可以參考下2017-03-03Canvas實(shí)現(xiàn)放射線動(dòng)畫效果
本文主要分享了Canvas實(shí)現(xiàn)放射線動(dòng)畫的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02