【问题标题】:How to make header sticky on mobile?如何使标题在手机上保持粘性?
【发布时间】:2020-08-17 11:31:21
【问题描述】:

目标

在将此响应式 HTML5 模板调整为手机尺寸时使标题保持粘性。

问题

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

在行动

桌面模式运行良好

手机尺寸不合格

尝试失败

我尝试过附加一个类,像这样...

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

...到每个元素,甚至是远离标题的地方。我就是找不到合适的,让它工作。

直播

这是在服务器上运行的页面: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 - 所以任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 关于 Stack Overflow 的问题必须是独立的,因此您需要包含所有相关代码,因为外部链接会随着时间的推移而改变或中断,从而使问题对其他用户没有帮助。请编辑您的问题以包含一个minimal,reproducible example,该minimal,reproducible example 也包含相关的 HTML,以便我们查看可能出现的问题并提供帮助。
  • 问题不在于html,而与js dom有关。我建议您编写自己的模板,以便您理解和编辑它们。
  • 好的,在这里发布代码。 @djangotic,正如我所说,我继承了这个项目。客户想要这个特定的模板。

标签: html css


【解决方案1】:

粘性位置需要空间,并且要使其工作,.header-area 类需要粘性。仅将此规则添加到您的媒体查询中!我刚试了一下,粘性位置是这样的:

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

【讨论】:

  • 这是移动视口的绝佳解决方案。它不包括桌面版本,但我明白你在做什么。感谢您的帮助,@sergey。我想知道,为什么高 z 指数?
  • 我很高兴回答你:) 一般来说,没有必要设置这么高的 z-index。此值是作为示例提供的。顺便说一句,9999 的值是最高的。
【解决方案2】:

移动导航需要指定position: fixed。桌面导航具有相同的属性,但也有一个自定义动画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 */
  }
}

输出:

【讨论】:

  • 是的!一些微小的修改,你的代码就做到了!谢谢。 @media screen and (max-width: 600px) { /* 只针对 600px 以下的设备 / .header-area { width: 100%;位置:固定; z指数:999; / 使其不在其他元素下/ background: #1c2454; / 它是透明的 / } .slider-area { top: 85px; / 可以根据自己的喜好改变 */ } }
  • 请问,简单地绕过现有的 CSS 规则,并在底部弹出一个可能会或可能不会覆盖现有规则的 @media 规则是“作弊”吗?我问这个是因为,它我确实想到在最后添加一个新规则(在添加一个新的类名之后),这将强制固定位置。我指的是我的想法,而不是你的解决方案。
  • 这不是作弊,而是解决我们的问题。这是一种方法,直到你有一个框架,他们为你提供现成的实用程序类来修复。即使那样作弊也很好。
猜你喜欢
  • 1970-01-01
  • 2020-11-18
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
相关资源
最近更新 更多