Bagaimana untuk Menghalang Elemen Kedudukan Tetap daripada Bertindih Pengaki?

Barbara Streisand
Lepaskan: 2024-11-14 16:24:01
asal
498 orang telah melayarinya

How to Prevent Fixed Position Elements from Overlapping the Footer?

Solving the Fixed Position Overlap at Footer Issue

When designing web pages with fixed position elements, it is common to encounter scenarios where these elements overlap with the page footer. To address this issue, a simple and effective jQuery solution can be implemented.

Identify the Elements

The html code provided defines the share box element (#social-float) and the CSS positions it at a fixed bottom left corner. The footer element (#footer) does not have a fixed height.

Handle Page Scrolling

To monitor the position of the share box relative to the footer, register a scroll event handler using jQuery's scroll() method.

$(document).scroll(function() {
    checkOffset();
});
Salin selepas log masuk

Check Share Box Offset

Inside the checkOffset() function, calculate the vertical offset of the share box in relation to the footer. If the offset is less than 10px, meaning the share box has encroached upon the footer, update its position to absolute.

function checkOffset() {
    if($('#social-float').offset().top + $('#social-float').height() 
                                           >= $('#footer').offset().top - 10)
        $('#social-float').css('position', 'absolute');
}
Salin selepas log masuk

Restore Fixed Position

When the user scrolls back up the page, restore the fixed position of the share box by setting its position back to fixed.

if($(document).scrollTop() + window.innerHeight < $('#footer').offset().top)
        $('#social-float').css('position', 'fixed');
Salin selepas log masuk

Ensure Sibling Elements

The parent of the share box (#social-float) should be a sibling of the footer (#footer). This allows for proper positioning relative to the footer.

<div class="social-float-parent">
    <div>
Salin selepas log masuk

By implementing this jQuery solution, the share box will remain fixed in place but will automatically stop before overlapping the footer, ensuring a clean and visually appealing design.

Atas ialah kandungan terperinci Bagaimana untuk Menghalang Elemen Kedudukan Tetap daripada Bertindih Pengaki?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan