我有一個 div,我想在頁面上保持相對位置,直到滾動位置到達 div 的頂部,然后通過添加類來應用固定浮動。我有我認為應該有效的代碼;但是,無法讓它做任何事情。滾動時,div 保持相對狀態,并且不會應用該類來修復對象。$(function() { var a = function() { var b = $(window).scrollTop(); var c = $("#header"); var d = c.offset(); if(b > d){ alert(d); c.addClass("header-fixed"); } else { c.removeClass("header-fixed"); }};});這是CSS.header-fixed { position: fixed; top: 0; left: 0; right: 0; }#header { z-index: 1000; float: left; width: 100%; z-index: 9999998;}我在標題上方有一個 div,其高度可能會波動,因此我想動態計算從標題頂部到頁面頂部的距離。每當滾動位置到達div頂部的位置時,我想添加 header-fixed 類。如果滾動位置小于該位置,我想刪除Class header-fixed 以再次顯示標題上方的div。HTML:<div id="header_container"><div id="header" class="background-white border-bottom-navy-dark box-shadow-navy-dark"><div id="header_1" ><a href="index" class="no-decoration"><img class="logo" src="images/12345.png" alt=""/><div class="display-none-mobile"><h1 class="title1 color-gold">HEADER 1</h1><h2 class="subtitle color-navy-dark">SUB HEADER</h2></div></div></a> <?php /*---- header left ----*/ ?><div id="header_2"><a href="javascript:void(0)" class="cart-link no-decoration color-gold material-icons">shopping_cart</a><a href="javascript:void(0)" class="account-link no-decoration color-gold material-icons">person</a><a href="javascript:void(0)" class="menu-link no-decoration color-gold material-icons">menu</a></div> <?php /*---- header right ----*/ ?></div> <?php /*---- header ----*/ ?></div> <?php /*---- header site container ----*/ ?></div> <?php /*---- header container ----*/ ?>
- 0 回答
- 0 關注
- 189 瀏覽
添加回答
舉報
0/150
提交
取消