jquery實現分享到側邊欄原理

來源:互聯網
上載者:User

標籤: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實現分享到側邊欄原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.