【问题标题】:Reveal elements on hover and element alignment in header with CSS and HTML使用 CSS 和 HTML 在标题中显示悬停元素和元素对齐
【发布时间】:2014-11-09 15:31:07
【问题描述】:

我的页面顶部有一个全宽的粘性页眉。在此标题上,徽标位于左侧,导航菜单位于中间,搜索图标位于最右侧。

我希望在将鼠标悬停在搜索图标上时出现一个弹出窗口。只要鼠标指针位于弹出窗口内或搜索图标上方,我希望弹出窗口一直可见。

我还想将弹出窗口的最右侧放在我的小搜索图标的最右侧。

HTML 代码:

<header id="header">
    <div class="container_header clearfix">

            <div class="menu-row-top clearfix">

            <div class="header-logo">
                <a href="http://www.mywebsite.com">
                    <img src="/small_logo.png" alt="logo" height="44" width="210">
                </a>
            </div>

            <!-- NAVIGATION -->
            <nav class="primary">
                  <?php wp_nav_menu( array(
                    'container'       => 'ul', 
                    'menu_class'      => 'sf-menu', 
                    'menu_id'         => 'topnav',
                    'depth'           => 0,
                    'theme_location' => 'header_menu' 
                    )); 
                  ?>
                </nav><!--.primary-->


            <div class="header-search">
                <a class="search"><img src="/search_icon.png" alt="search_icon" height="28" width="28"></a>
                    <div class="header-search-form">
                        This is the search form
                    </div>
            </div>

    </div>
</header>

CSS 代码:

.container_header{
background: #0459b5; 
}

.menu-row-top {
    margin: 0 auto; 
    max-width: 960px;
   height:44px;
}

.header-logo{
float: left;
display:block;
}

nav.primary {
  position: relative;
  z-index: 2;
  display: block;
  float: left;
  margin-left: 20px;
}

.header-search{
float:right;
display:block;
margin-top:7px;
margin-left: 12px;
}

.header-search-form{
    display:none;
    position: fixed;
    top: 44px;
    background: white;
    width: 400px;
    border-bottom: solid 4px #036dda;
    height: 80px;
}


a.search:hover + .header-search-form{
display: block;
}

使用上面的代码,当我的鼠标悬停在搜索图标上时,会出现带有搜索表单的弹出窗口,但如果我悬停搜索表单,则不会保持可见。我想获得正确的 CSS 标记。

此外,弹出窗口从搜索图标的左侧一直对齐到我的屏幕右侧。我尝试使用位置(左,右)进行一些操作,但它似乎将窗口一直发送到我屏幕的两个极端,即 960px 宽度之外。无论我如何调整浏览器窗口的大小(即从我的搜索图标的右侧从右到左对齐),我也希望窗口始终保持在同一位置。

【问题讨论】:

    标签: html css header alignment


    【解决方案1】:

    我设法让你的弹出窗口工作我在你的 css http://jsfiddle.net/ce1j6k7k/3/ 中编辑了这些

    .header-search-form {
    z-index:1000;
    display:none;
    position: fixed;
    top: 44px;
    background: green;
    width: 400px;
    border-bottom: solid 4px #036dda;
    height: 80px;
    }
    .header-search:hover  .header-search-form {
    display: block;
    }
    

    我给了一个更高的z-index 值,所以它总是在最前面

    【讨论】:

    • 由于某种原因,它只会在我第一次悬停图标时起作用,然后,当鼠标悬停在弹出窗口上时它会停止正常工作。我必须重新加载页面才能让它再次工作......
    • 它在您发送的链接中有效,但不幸的是它不在我的网站上。它只能工作 1 次,然后我必须刷新页面才能让它再次工作!
    • 它将弹出窗口一直发送到我的屏幕右侧,这绝对是不希望的,因为它超出了我想要保留我的东西的 960px 宽度。基本上,我想要弹出窗口的最右侧与小图标的最右侧对齐。使用我的问题中列出的代码,弹出窗口的左边框与我的图标的左侧对齐。
    • @LaGuille 我将right:0 更改为` margin-left:-250px;` 现在它似乎停留在jsfiddle.net/ce1j6k7k/5
    • 现在完美运行! +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多