標籤:var document scrolltop nbsp blog bsp pad ack style
幾乎每一個網站,都有一個分享到側邊欄的功能,
我寫的這個是固定在網頁視窗的左側,
html:
<!--------------分享到側邊欄----------------------><div id="share"> <span>分享</span></div>
css:
/*******************************分享到側邊欄**************************************/#share{ position: absolute; left:-100px; top: 50px; width: 100px; height: 100px; background-color: red; z-index: 2;}#share span{ position: absolute; left:100px; top:30px; width:20px; height: 37px; padding-top:5px; display: block; background-color: purple; line-height: 14px; font-size: 14px; text-align: center;}
那麼問題來了,怎麼固定在網頁的中部?
首先擷取到視窗的捲軸的到文檔頂部的距離,再加上可視地區減去分享到側邊欄的高度除2,就是需要分享到側邊欄的高度。
剩下的就是滑鼠移入,側邊欄向右移動。移出縮排。
jquery:
$(function(){ $(window).scroll(function(){ var scrolltop=($(window).height()-100)/2; var top=$(document).scrollTop()+scrolltop; $("#share").css("marginTop",top+"px"); /************把分享欄固定在視窗左側***********/}) /************移入移出側邊欄***********/ $("#share").hover(function(){ $(this).animate({left: 0 },300); }, function(){ $(this).animate({left:-100},300); } );
jquery實現分享到側邊欄原理