You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

滚动时网站Logo缩小至粘性菜单实现方案咨询

Hey there! Let's tackle this sticky logo resize effect you're after—super common and totally doable with a mix of HTML, CSS, and a touch of JavaScript. Here's a step-by-step breakdown that's easy to follow:

1. Basic HTML Structure

First, set up your header and logo elements. We'll use a single logo element and toggle a CSS class to switch its state seamlessly:

<header class="site-header">
  <div class="logo">
    <img src="your-logo.png" alt="Site Logo">
  </div>
</header>

<main class="content">
  <!-- Your page content goes here—make sure it's long enough to scroll! -->
  <p>Lorem ipsum dolor sit amet... (add plenty of text to test scrolling)</p>
</main>
2. CSS for Default & Sticky States

Style the header and logo for both the initial centered, large state and the sticky, resized state. We'll add smooth transitions so the change feels natural instead of jarring:

/* Default header styles */
.site-header {
  position: relative;
  width: 100%;
  padding: 2rem 0;
  background: white;
  transition: all 0.3s ease; /* Smooth transition for padding/position changes */
}

/* Default logo: centered, large size */
.logo {
  display: flex;
  justify-content: center;
}

.logo img {
  width: 200px; /* Adjust to your large logo size */
  height: auto;
  transition: width 0.3s ease; /* Smooth resize animation */
}

/* Sticky header styles */
.site-header.sticky {
  position: fixed;
  top: 0;
  left: 0;
  padding: 0.5rem 2rem; /* Smaller padding when sticky */
  box-shadow: 0 2px 5px rgba(0,0,0,0.1); /* Optional: add shadow for depth */
  z-index: 1000; /* Ensure header stays above other content */
}

/* Sticky logo: smaller, aligned left */
.site-header.sticky .logo {
  justify-content: flex-start;
}

.site-header.sticky .logo img {
  width: 100px; /* Adjust to your small sticky logo size */
}
3. JavaScript to Toggle the Sticky Class

We need to listen for scroll events and switch the sticky class on the header when the user scrolls past the header's initial height. Adding debouncing prevents excessive event triggers for better performance:

const header = document.querySelector('.site-header');
const headerHeight = header.offsetHeight;

// Debounced scroll listener to avoid performance issues
let debounceTimer;
window.addEventListener('scroll', () => {
  clearTimeout(debounceTimer);
  debounceTimer = setTimeout(() => {
    // Toggle class based on scroll position
    if (window.scrollY > headerHeight) {
      header.classList.add('sticky');
    } else {
      header.classList.remove('sticky');
    }
  }, 10); // Adjust delay as needed (10ms is a good default)
});
Quick Tips to Polish It Up
  • Image Optimization: Use a high-res logo that scales down cleanly—avoid pixelation by saving it in a vector format (like SVG) if possible.
  • Responsive Tweaks: Add media queries to adjust logo sizes and header padding for mobile devices, ensuring the sticky menu works well across screen sizes.
  • Customize Animations: Play with transition values (e.g., 0.4s ease-in-out) to make the resize faster, slower, or more fluid.
  • Content Offset: If your fixed header covers top content when sticky, add padding-top: [header-height] to your main.content element to prevent content from being hidden.

That's it! Test it out—scroll down and watch the logo shrink into a sticky menu, then scroll back up to see it return to the centered large state.

内容的提问来源于stack exchange,提问作者Alex Priddle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:39:58