繁体   English   中英

如何让 header 在手机上保持粘性?

[英]How to make header sticky on mobile?

目标

当将此响应式 HTML5 模板调整为手机尺寸时,使 header 具有粘性。

问题

我似乎找不到正确的元素来添加 CSS position 属性。 无论我将“粘性” class 添加到哪个 DIV,它都不会固定在屏幕顶部。

在行动

桌面模式工作正常

作品

移动尺寸失败

不粘

失败的尝试

我试图附加一个 class,像这样......

.makeSticky {
  position: -webkit-sticky; /* Safari */
  position: sticky;
  top: 0;
}

...对于每个元素,甚至是远离 header。 我只是找不到合适的,并让它工作。

居住

这是在服务器上运行的页面: http://stpete.epizy.com/en/index.html

代码

  <header class="header-area header-3">
        <div class="desktop-nav d-none d-lg-block">
            <div class="header-nav">
                <div class="container-fluid custom-container">
                    <div class="header-nav-wrapper d-flex justify-content-between">
                        <div class="header-static-nav">
                            <p>Get FREE Shipping with <span class="text">$35!</span> Code: FREESHIPPING</p>
                        </div>
                        <div class="header-menu-nav">
                            <ul class="menu-nav">
                               <li>
                                    <div class="dropdown">
                                        <button type="button" id="setting" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Setting <i class="icon ion-chevron-down"></i></button>

                                         <ul class="dropdown-menu" aria-labelledby="setting">
                                          
                                            <li><a href="checkout.html">Checkout</a></li>
                                            <li><a href="login.html">Sign in</a></li>
                                        </ul>
                                    </div>
                                </li>
                              
                                <li>
                                    <div class="dropdown">
                                        <button type="button" id="langue" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><img src="images/1.jpg" alt=""> English <i class="icon ion-chevron-down"></i></button>

                                        <ul class="dropdown-menu" aria-labelledby="langue">
                                            <li><a href="http://123globalelecronicsllc.com/en"><img src="images/1.jpg" alt=""> English</a></li>
                                            <li><a href="http://123globalelecronicsllc.com/sp"><img src="images/2.jpg" alt=""> Spanish</a></li>
                                        </ul>
                                    </div>
                                </li>
                            </ul>
                        </div> <!-- header menu nav -->
                    </div> <!-- header nav wrapper -->
                </div> <!-- container -->
            </div> <!-- header nav -->

            <div class="header-middle">
                <div class="container-fluid custom-container">
                    <div class="row">
                        <div class="col-lg-3">
                            <div class="header-logo-contact d-flex  ">
                                <div class="desktop-logo">
                                    <a href="index.html">
                                        <img src="images/logo-white.png" alt="Global Eletronics, LLC">
                                    </a>
                                </div> <!-- desktop logo -->
                            </div>
                        </div>
                        <div class="col-lg-9">
                            <div class="header-search-cart  d-flex align-items-center">
                                <div class="header-contact d-flex align-items-center">
                                    <i class="icon ion-ios-telephone-outline"></i>
                                    <div class="phone media-body">
                                        <p>24/7 Support</p>
                                        <a href="tel:+7276230753">+727-623-0753</a>
                                    </div>
                                </div>
                                <div class="header-search media-body">
                                    <form action="#">
                                        <div class="search-category">
                                            <select>
                                                <option value="">All categories</option>
                                                <option value="audio">Audio Parts</option>
                                                <option value="accessories">- - Accessories</option>
                                                <option value="buzzers">- - Buzzers</option>
                                                <option value="batteries">Battery Products</option>
                                                <option value="batteries">- - Primary Batteries</option>
                                                
                                            </select>
                                        </div>
                                        <input  id="search-box" type="text" placeholder="Part #">
                                        <button><i class="icon ion-android-search"></i></button>
                                    </form>
                                </div>
                                <div class="header-cart">
                                    <div class="cart-btn">
                                        <a href="cart.html">
                                            <i class="icon ion-calculator"></i>
                                            <span class="text">RFQ Parts :</span>
                                           
                                            <span class="count">0</span>
                                        </a>
                                    </div>
                                    <!--
                                    <div class="mini-cart">
                                        <ul class="cart-items">
                                            <li>
                                                <div class="single-cart-item d-flex">
                                                    <div class="cart-item-thumb">
                                                        <a href="cart.html"><img src="images/cart-1.jpg" alt="product"></a>
                                                        <span class="product-quantity">1x</span>
                                                    </div>
                                                   
                                                </div>
                                            </li>
                                           
                                        </ul>
                                        <div class="price_content">
                                            <div class="cart-subtotals">
                                                
                                            </div>
                                            <div class="cart-total price_inline">
                                                <span class="label"># Items</span>
                                                <span class="value">12</span>
                                            </div>
                                        </div> 
                                        <div class="checkout text-center">
                                            <a href="checkout.html" class="main-btn">Send RFQ</a>
                                        </div>
                                    </div> 
                                -->
                                </div>
                            </div> <!-- header search cart -->
                        </div>
                    </div> <!-- row -->
                </div> <!-- container -->
            </div> <!-- header middle -->

            <div class="header-menu">
                <div class="container-fluid custom-container">
                    <div class="row">
                        <div class="col-lg-3">
                            <div class="header-menu-vertical">
                                <h4 class="menu-title">
                                    <span>Parts by</span>
                                    Category
                                </h4>
                                <ul class="menu-content menu-expand">
                                    <li class="menu-item">
                                        <a href="products.html?cat=semiconductor">Semiconductor </a>
                                       
                                    </li>
                                    <li class="menu-item">
                                        <a href="products.html?cat=semiconductor">Passives </a>
                                     
                                    </li>
                                    <li class="menu-item"><a href="products.html?cat=semiconductor">ElectroMechanical</a></li>
                                    <li class="menu-item"><a href="products.html?cat=semiconductor">Power & Circuit</a></li>
                                    <li class="menu-item"><a href="products.html?cat=semiconductor">Automation</a></li>
                                    <li class="menu-item"><a href="products.html?cat=semiconductor">Connectors</a></li>
                                    <li class="menu-item"><a href="products.html?cat=semiconductor">Cables & Wires</a></li>
                                    <li class="menu-item"><a href="products.html?cat=semiconductor">Test</a></li>
                                 
                                   </ul> <!-- menu content -->
                            </div> <!-- header menu vertical -->
                        </div>
                        <div class="col-lg-9 position-static">
                            <div class="header-horizontal-menu">
                                <ul class="menu-content">
                                    <li class="active"><a href="index.html">Home </a>
                                    
                                    </li> <li class="position-static"><a href="products.html?cat=all">Products <i class="fal fa-chevron-down"></i></a>
                                        <ul class="mega-sub-menu d-flex flex-wrap">
                                            <li>
                                                <a class="menu-title" href="#">Shop Grid</a>
                                                <ul class="submenu-item">
                                                    <li><a href="shop-3-column.html">Shop Grid Column 3</a></li>
                                                    <li><a href="shop-4-column.html">Shop Grid Column 4</a></li>
                                                    <li><a href="shop-left-sidebar.html">Shop Grid left sidebar</a></li>
                                                    <li><a href="shop-right-sidebar.html">Shop Grid Right sidebar</a></li>
                                                </ul>
                                            </li>
                                            <li>
                                                <a class="menu-title" href="#">Shop List</a>
                                                <ul class="submenu-item">
                                                    <li><a href="shop-list.html">Shop List</a></li>
                                                    <li><a href="shop-list-left-sidebar.html">Shop List Left sidebar</a></li>
                                                    <li><a href="shop-list-right-sidebar.html">Shop List Right sidebar</a></li>
                                                </ul>
                                            </li>
                                            <li>
                                                <a class="menu-title" href="#">Shop Single</a>
                                                <ul class="submenu-item">
                                                    <li><a href="single-product.html">Shop Single</a></li>
                                                    <li><a href="single-product-variable.html">Shop Variable</a></li>
                                                    <li><a href="single-product-affiliate.html">Shop Affiliate</a></li>
                                                    <li><a href="single-product-group.html">Shop Group</a></li>
                                                    <li><a href="single-product-tabstyle2.html">Shop Tabs 2</a></li>
                                                    <li><a href="single-product-tabstyle3.html">Shop Tabs 3</a></li>
                                                </ul>
                                            </li>
                                            <li>
                                                <a class="menu-title" href="#">Shop Single</a>
                                                <ul class="submenu-item">
                                                    <li><a href="single-product-slider.html">Shop Slider</a></li>
                                                    <li><a href="single-product-gallery-left.html">Shop Gallery Left sidebar</a></li>
                                                    <li><a href="single-product-gallery-right.html">Shop Gallery Right sidebar</a></li>
                                                    <li><a href="single-product-sticky-left.html">Shop Sticky Left sidebar</a></li>
                                                    <li><a href="single-product-sticky-right.html">Shop Sticky Right sidebar</a></li>
                                                </ul>
                                            </li>
                                            <li class="custom-banner">
                                                <a href="shop-4-column.html"><img src="images/custom_banner.jpg" alt="" class="img-responsive"></a>
                                            </li>
                                        </ul>
                                    </li>
                                   
                                    <li><a href="#">Services <i class="fal fa-chevron-down"></i></a>
                                        <ul class="sub-menu">
                                            <li><a href="about.html">About</a></li>
                                           
                                        </ul>
                                    </li>
                                   
                                  
                                    <li><a href="#">Resources <i class="fal fa-chevron-down"></i></a>
                                        <ul class="sub-menu">
                                            <li><a href="#">BOM Upload Tool</a></li>
                                            <li><a href="#">Global Learning Lab</a></li>
                                            <li><a href="#">Product Advisor</a></li>
                                            <li><a href="#">Articles</a></li>
                                </ul>
                                    <li><a href="about.html">About</a></li>
                                    <li><a href="contact.html">Contact</a></li>
                                </ul>
                            </div> <!-- header horizontal menu -->
                        </div>
                    </div> <!-- row -->
                </div> <!-- container -->
            </div> <!-- header menu -->
        </div> <!-- desktop nav -->

        <div class="mobile-nav d-lg-none">
            <div class="container-fluid">
                <div class="mobile-nav-top">
                    <div class="row align-items-center">
                        <div class="col-sm-4 col-3">
                            <div class="mobile-toggle">
                                <a class="mobile-menu-open" href="javascript:;"><i class="fal fa-bars"></i></a>
                            </div>
                        </div>
                        <div class="col-sm-4 col-5">
                            <div class="mobile-logo text-center">
                                <a href="index.html">
                                    <img src="images/logo-white.png" alt="Logo">
                                </a>
                            </div> <!-- mobile logo -->
                        </div>
                        <div class="col-sm-4  col-4">
                            <div class="mobile-account-cart">
                                <ul class="account-cart text-right">
                                    <li>
                                        <div class="dropdown">
                                            <button type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fal fa-user"></i></button>

                                            <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                                <li><a href="#">My account</a></li>
                                                <li><a href="#">Checkout</a></li>
                                                <li><a href="#">Sign in</a></li>
                                            </ul>
                                        </div>
                                    </li>
                                    <li class="header-cart">
                                        <a href="#">
                                            <i class="fal fa-envelope-open-dollar"></i>
                                            <span>0</span>
                                        </a>
                                        <div class="mini-cart">
                                            <ul class="cart-items">
                                                <li>
                                                    <div class="single-cart-item d-flex">
                                                        <div class="cart-item-thumb">
                                                            <a href="single-product.html"><img src="images/cart-1.jpg" alt="product"></a>
                                                            <span class="product-quantity">1x</span>
                                                        </div>
                                                        <div class="cart-item-content media-body">
                                                            <h5 class="product-name"><a href="single-product.html">New Balance Fresh Foam LAZR</a></h5>
                                                            <span class="product-price">€18.90</span>
                                                            <span class="product-color"><strong>Color:</strong> White</span>
                                                            <a href="#" class="product-close"><i class="fal fa-times"></i></a>
                                                        </div>
                                                    </div>
                                                </li>
                                                <li>
                                                    <div class="single-cart-item d-flex">
                                                        <div class="cart-item-thumb">
                                                            <a href="single-product.html"><img src="images/cart-2.jpg" alt="product"></a>
                                                            <span class="product-quantity">3x</span>
                                                        </div>
                                                        <div class="cart-item-content media-body">
                                                            <h5 class="product-name"><a href="single-product.html">New Balance Fresh Foam LAZR</a></h5>
                                                            <span class="product-price">€18.90</span>
                                                            <span class="product-color"><strong>Color:</strong> White</span>
                                                            <a href="#" class="product-close"><i class="fal fa-times"></i></a>
                                                        </div>
                                                    </div>
                                                </li>
                                            </ul>
                                            <div class="price_content">
                                                <div class="cart-subtotals">
                                                    <div class="products price_inline">
                                                        <span class="label">Subtotal</span>
                                                        <span class="value">€30.80</span>
                                                    </div>
                                                    <div class="shipping price_inline">
                                                        <span class="label">Shipping</span>
                                                        <span class="value">€7.00</span>
                                                    </div>
                                                    <div class="tax price_inline">
                                                        <span class="label">Taxes</span>
                                                        <span class="value">€0.00</span>
                                                    </div>
                                                </div>
                                                <div class="cart-total price_inline">
                                                    <span class="label">Total</span>
                                                    <span class="value">€37.80</span>
                                                </div>
                                            </div> <!-- price content -->
                                            <div class="checkout text-center">
                                                <a href="checkout.html" class="main-btn">Checkout</a>
                                            </div>
                                        </div> <!-- mini cart -->
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div> <!-- row -->
                </div> <!-- mobile nav top -->

                <div class="header-search">
                    <form action="#">
                        <div class="search-category">
                            <select>
                                <option value="0">All categories</option>
                                <option value="12">Laptop</option>
                                <option value="13">- - Hot Categories</option>
                                <option value="19">- - - - Dresses</option>
                                <option value="20">- - - - Jackets &amp; Coats</option>
                                <option value="21">- - - - Sweaters</option>
                                <option value="22">- - - - Jeans</option>
                                <option value="23">- - - - Blouses &amp; Shirts</option>
                              
                            </select>
                        </div>
                        <input type="text" placeholder="Enter your search key ... ">
                        <button><i class="icon ion-android-search"></i></button>
                    </form>
                </div>
            </div> <!-- container -->

            <div class="mobile-off-canvas-menu">
                <div class="mobile-canvas-menu-top">
                    <ul class="menu-nav">
                        <li><a href="compare.html"><i class="fal fa-repeat"></i> Compare (0)</a></li>
                        <h6 class="custom-title">Women is Clothes &amp; Fashion</h6>
                                                <p>Shop women is clothing and accessories and get inspired by the latest fashion trends.</p>
                                            </div>
                                        </div>
                                        <div class="col-lg-4 col-md-12">
                                            <div class="menu-block">
                                                <h6 class="custom-title">Simple Style</h6>
                                                <p>A new flattering style with all the comfort of our linen.</p>
                                            </div>
                                        </div>
                                        <div class="col-lg-4 col-md-12">
                                            <div class="menu-block">
                                               
                               
                                    </ul>
                                </li>
                                <li class="menu-item-has-children">
                                    <a href="#"><span>Outerwear & Jackets</span></a>
                                
                                        <li><a href="shop-left-sidebar.html">Bags & Cases</a></li>
                                     
                    </ul> <!-- menu content -->
                </div> <!-- mobile main menu -->
            </div> <!-- mobile off canvas menu -->
            <div class="overlay"></div>
        </div> <!-- mobile nav -->
    </header>

CSS

/*===== header 3 =====*/
.header-3 {
  background-color: #1c2454; }
  .header-3 .header-nav {
    background-color: #000;
    border-bottom: 0; }
    .header-3 .header-nav .header-nav-wrapper .header-static-nav p {
      color: #fff; }
      .header-3 .header-nav .header-nav-wrapper .header-static-nav p .text {
        color: #fff; }
    .header-3 .header-nav .header-nav-wrapper .header-menu-nav .menu-nav li + li {
      margin-left: 30px; }
      @media only screen and (min-width: 992px) and (max-width: 1199px) {
        .header-3 .header-nav .header-nav-wrapper .header-menu-nav .menu-nav li + li {
          margin-left: 20px; } }
      .header-3 .header-nav .header-nav-wrapper .header-menu-nav .menu-nav li + li::before {
        color: rgba(255, 255, 255, 0.4); }
    .header-3 .header-nav .header-nav-wrapper .header-menu-nav .menu-nav li a {
      color: #fff; }
    .header-3 .header-nav .header-nav-wrapper .header-menu-nav .menu-nav li .dropdown button {
      color: #fff; }
  .header-3 .header-contact i {
    color: #fff; }
  .header-3 .header-contact .phone p {
    color: #fff; }
  .header-3 .mobile-toggle a {
    color: #fff; }
  .header-3 .mobile-account-cart .account-cart li .dropdown button {
    color: #fff; }
    .header-3 .mobile-account-cart .account-cart li .dropdown button i {
      font-size: 24px; }
  .header-3 .mobile-account-cart .account-cart li a {
    color: #fff; }
  .header-3 .header-menu {
    background-color: #1c2454;
    border-top: 1px solid rgba(255, 255, 255, 0.4); }
    .header-3 .header-menu.sticky {
      border-top: 0; }

我继承了这个项目,HTML是从某个模板公司购买的。 我没有写 HTML - 所以任何帮助将不胜感激。

谢谢你。

粘性 position 需要空间,为此, .header-area class 需要粘性。 只需将此规则添加到您的媒体查询中,我刚刚尝试过:粘性 position 的工作方式如下:

.header-area {
    position: sticky;
    top: 0;
    z-index: 9999;
}

您需要指定position: fixed为移动导航。 桌面导航具有相同的属性,但也有一个自定义的 animation sticky 您可能还想调整它下面的元素,因为菜单现在是固定的。

@media and (max-width: 600px) { /* Target only devices below 600px */
  .header-area {
    position: fixed;
    z-index: 999; /* So that it is not under the other elements */
    background: #1c2454; /* It was transparent */
  }

  .slider-area {
    top: 85px; /* Can change according to your liking */
  }
}

Output:

移动头

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM