2017-02-14 36 views
0

假設我正在製作帶有兩個col-lg-6 div的bootstrap 4佈局。左邊的圖片包含一個圖片,右邊的圖片包含一串文字,足以讓頁面可以滾動。 由於引導程序4中的網格基於柔性顯示屬性,因此左div會自動垂直延伸至右邊的高度。 Bootstrap 4有一個新的「sticky-top」類,它使用position:sticky。 因此,如果向左側div內的圖像添加「sticky-top」類並且向下滾動頁面,則圖像會隨頁面一起滾動,直至到達頁面頂部,然後粘到頂部並保持粘性直到其父div的底部到達圖像的底部,然後圖像繼續與頁面一起滾動。 不幸的是,在所有現代瀏覽器中仍然沒有完全支持position:sticky,所以我想知道是否有跨瀏覽器兼容的jQuery替代方案? 我試圖通過jquery添加一個額外的類到圖像,將位置更改爲固定在CSS中,並在圖像到達頁面頂部時添加到圖像中,然後嘗試在頁腳進入視口後將其刪除,但是這會使圖像從視口中消失,而不是隨着頁面一起滾動,因爲它會在附加類從其中移除後跳回到其父級頂部。是否有替代位置的jQuery:粘性?

<header>header sticks to top</header> 
<div>breadcrumbs that scroll along with the page go here</div> 
<div class="row"> 
    <div class="col-12 col-lg-6"> 
    <img src="image.jpg" class="img-fluid" alt="image"> 
    </div> 
    <div class="col-12 col-lg-6"> 
    <p>Bunch of random text long enough to make the page scrollable goes here</p> 
    </div> 
</div> 
<footer>Footer stuff goes here</footer> 

注意:圖像應該只粘在大的和特大的網格上,我寧願不必使用任何插件。

+0

是啊,這是用JavaScript(即使你在一些jQuery的撒。)在某些條件下完全有可能,使其固定或不固定的。 –

+0

你用'sticky-top'試過了嗎?如果是這樣,請發佈代碼,以便我瞭解您想要做出粘性的事情。 – ZimSystem

+0

我到目前爲止所做的一切都是廢話,所以不值得發表。我需要在頁面滾動後將圖片粘貼到頂部,並在頁腳到達視口後取消粘貼。基本上和他們在這個網頁上的apple.com上的是一樣的http://www.apple.com/shop/buy-mac/macbook-pro?product=MLH42LL/A&step=config# –

回答

2

我寫得很快。將類.sticky添加到您想粘的東西,並添加您希望它粘住的父類.stickyTo。這不是完美的,但給出了一般的概念,也許你可以調整它爲你的項目。

var $sticky = $('.sticky'), 
 
    $stickyTo = $('.stickyTo'), 
 
    stickyToTop = $stickyTo.offset().top, 
 
    stickyToBottom = stickyToTop + $stickyTo.outerHeight(); 
 

 
$(window).on('scroll', function() { 
 
    var scroll = $(window).scrollTop(), 
 
    stickyTop = $sticky.offset().top, 
 
    stickyBottom = $sticky.offset().top + $sticky.outerHeight(); 
 
    
 
    if (stickyBottom >= stickyToBottom) { 
 
    if (scroll < stickyTop) { 
 
     $sticky.addClass('fixed').removeClass('abs'); 
 
    } else { 
 
     $sticky.addClass('abs'); 
 
    } 
 
    } else if (scroll > stickyToTop) { 
 
    $sticky.addClass('fixed'); 
 
    } else if (scroll < stickyToTop) { 
 
    $sticky.removeClass('abs').removeClass('fixed'); 
 
    } 
 
});
.row { 
 
    background: #ccc; 
 
} 
 

 
.fixed { 
 
    position: fixed; 
 
    top: 0; 
 
    bottom: auto; 
 
} 
 

 
.abs { 
 
    position: absolute; 
 
    bottom: 0; 
 
    top: auto; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"> 
 
<header>header sticks to top</header> 
 
<div>breadcrumbs that scroll along with the page go here</div> 
 
<div class="row stickyTo"> 
 
    <div class="col-12 col-lg-6"> 
 
    <img src="http://kenwheeler.github.io/slick/img/fonz1.png" class="img-fluid sticky" alt="image"> 
 
    </div> 
 
    <div class="col-12 col-lg-6"> 
 
    <p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p> 
 
    </div> 
 
</div> 
 
<footer>Footer stuff goes here</footer>

+0

謝謝。我把它變成一個函數,並在$(document).ready和$(window).resize上調用它,以防止瀏覽器調整響應網格的錯誤。 –

+0

@GoranTesic很好,很高興它幫助。 –