從用博客開始,發現博客園中很多博友的博客中在Page右下角都有個圖標,不論屏幕怎么拉伸,都始終停留在右下角。點擊后頁面置頂。后面想想寫一個Demo來實現這種效果吧。
一. 圖標右下角固定.
1.SS 里面提供了4中布局方式. 其中fixed表示絕對定位元素。所以我們選擇使用fixed來實現圖標固定.
absolute | 生成絕對定位的元素,相對於 static 定位以外的第一個父元素進行定位。 元素的位置通過 "left", "top", "right" 以及 "bottom" 屬性進行規定。 |
fixed | 生成絕對定位的元素,相對於瀏覽器窗口進行定位。 元素的位置通過 "left", "top", "right" 以及 "bottom" 屬性進行規定。 |
relative | 生成相對定位的元素,相對於其正常位置進行定位。 因此,"left:20" 會向元素的 LEFT 位置添加 20 像素。 |
static | 默認值。沒有定位,元素出現在正常的流中(忽略 top, bottom, left, right 或者 z-index 聲明)。 |
inherit | 規定應該從父元素繼承 position 屬性的值。 |
2.定代碼如下。Button按鈕將始終置於屏幕右下角。不論是拖動上下精度條還是拉伸瀏覽器窗口大小.
#FourLeafCloverZCTopBtn{
bottom: 5px;
right: 5px;
position:fixed;
}
二. 實現點擊后回到頁面最上角.
1. 要想回到屏幕最上角就得小了解如何通過JavaScript還操作拖動條的上下移動.JavaScript提供了scrollby和scroll方法.
window.scrollBy(0,-30) //屏幕上移30像素點 window.scroll(0,0) // 屏幕回到最上角
2. 上面已經提到了如何移動拖動條,那么如何實現按照一定的速度移動到page頁頂部呢。那么就要借助setInterval和clearInterval方法. 實現沒10毫秒屏幕上移30個像素點。
<button id="FourLeafCloverZCTopBtn" onclick="FourLeafCloverZCTopFunc()">TOP</button>
<script type="text/javascript"> var FourLeafCloverZCVar; function FourLeafCloverZCTopFunc(){ FourLeafCloverZCVar=setInterval(FourLeafCloverZCEachScrollBy,10); } function FourLeafCloverZCEachScrollBy(eachHeight){ if(document.documentElement && document.documentElement.scrollTop) //IE { if(document.documentElement.scrollTop<=0){ clearInterval(FourLeafCloverZCVar); }else{ window.scrollBy(0,-30); } }else{ //Chrome不支持documentElement.scrollTop if(document.body.scrollTop<=0){ clearInterval(FourLeafCloverZCVar); }else{ window.scrollBy(0,-30); } } } </script>
三. 擴展
實現了置頂按鈕。那么我們如何實現點擊按鈕屏幕置底呢.其實原理差不多,這里就不寫demo了。給大家提供一些屬性做參考.
網頁可見區域寬:document.body.clientWidth 網頁可見區域高:document.body.clientHeight 網頁可見區域寬:document.body.offsetWidth (包括邊線的寬) 網頁可見區域高:document.body.offsetHeight (包括邊線的寬) 網頁正文全文寬:document.body.scrollWidth 網頁正文全文高:document.body.scrollHeight 網頁被卷去的高:document.body.scrollTop 網頁被卷去的左:document.body.scrollLeft 網頁正文部分上:window.screenTop 網頁正文部分左:window.screenLeft 屏幕分辨率的高:window.screen.height 屏幕分辨率的寬:window.screen.width 屏幕可用工作區高度:window.screen.availHeight 屏幕可用工作區寬度:window.screen.availWidth |
四. Demo http://files.cnblogs.com/files/FourLeafCloverZc/%E5%AF%BC%E8%88%AA.zip