首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CSS/JQuery:当鼠标悬停在另一个列表项上时,菜单列表标题下拉

CSS/JQuery:当鼠标悬停在另一个列表项上时,菜单列表标题下拉
EN

Stack Overflow用户
提问于 2018-07-27 04:53:30
回答 1查看 212关注 0票数 0

每当我将鼠标悬停在“博客”选项上时,我都会看到一个包含3个项目的列表。悬停本身功能完美:列表子项显示在列表项的下方。但是,菜单项所在的div会向下展开,菜单项标题会向下扩展到div的最底部,而悬停的列表项则保持在原来的位置。有谁有什么想法吗?谢谢!

代码:

    //dropdown hover code
    $(document).ready(function()
    {
    
    	//hover menu bar list
    	$('li').hover(function()
    	{
    		//find children of list items
    		//add stop function to stop hover queue
    		$(this).find('ul>li').stop().fadeToggle(300);
    	});
    	//end dropdown hover
    
    	//menu background color hover
    	$('a').css('background-color', 'white').hover(function()
    	{
    		$(this).css('background-color', 'pink');
    	},
    	function()
    	{
    		$(this).css('background-color', 'white');
    	});
    	//end background color hover
    
    });
    //end ready function
    @media (max-width: 480px) {
    	#dropdown_menu li {
    		display: block !important;
    		border: 1px solid black !important;
    		border-right: none !important;
    		border-left: none !important;
    
    	}
    
    	#title_img_container {
    		margin-bottom: 10%;
    	}
    
    	#title_container {
    		margin-top: 0 !important;
    		margin-bottom: 10%;
    		position: relative !important;
    	}
    
    	#dropdown_menu li {
    		border-bottom: none !important;
    
    	}
    }
    
    /*Change size of title div at 650px*/
    @media (max-width: 650px) {
    	#title_container {
    		font-size: 3em !important;
    	}
    }
    
    /*Change size of title img at 807px*/
    @media (min-width: 807px) {
    	#title_img_container {
    		margin-top: 3%;
    	}
    
    	#title_img {
    		max-width: 650px;
    		height: auto;
    	}
    }
    
    /*Change size of title img at 1300px*/
    @media (min-width: 1300px) {
    	#title_img {
    		max-width: 600px;
    		max-height: 700px;
    	}
    }
    
    body {
    	/*background-color: #dee1e5;*/
    	background-image: url("./../img/title_chevron.jpg");
    	letter-spacing: 1px;
    }
    
    #title_img_container {
    	max-width: 100%;
    	height: auto;
    }
    
    #title_img {
    	border: solid 1px black; 
    	width: 100%;
    	height: auto;
    	box-shadow: 2px 2px 2px 5px #888888;
    }
    
    #title_container {
    	border: solid 2px black;
    	width: 100%;
    	font-family: 'Lobster', cursive;
    	font-size: 5em;
    	color: gold;
    	text-align: center;
    	background-color: white;	
    }
    
    #menu_container {
    	font-size: 30px;
    }
    
    #img_and_content {
    	margin: 5%;
    }
    
    #dropdown_menu,
    #dropdown_menu ul {
    	margin: 0;
    	padding: 0;
    	list-style: none;
    }
    
    #dropdown_menu li{
    	display: inline-block;
    }
    
    #dropdown_menu a {
    	padding: 10px 20px;
    	display: block;
    	color: inherit;
    	text-decoration: none;
    }
    
    #dropdown_menu ul li {
    	display: none;
    	background-color: #afdbcd !important;
    	vertical-align: top;
    }
    
    li a {
    	color: gold;
    }
    
    #main_page_container {
    	padding: 2% 5% 2% 5%;
    	border: black 1px solid;
    	font-family: 'Helvetica', Arial;
    	background-color: white;
    }
    
    #content_title {
    	font-family: 'Lobster', cursive;
    }
    
    #footer_container {
    	color: black;
    	text-align: center;
    }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--FONTS-->
<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet">

<!--TITLE DIV-->
<div id="title_container">YOUR NAME
  <!--MENU DIV-->
  <div id="menu_container">
    <!--DROPDOWN MENU-->
    <ul id="dropdown_menu">
      <li><a href="#">Home</a></li> 
      <li><a href="final_about.html">About</a></li> 
      <!--HOVER MENU OPTIONS-->
      <li class="blogs"><a href="#">Blogs</a>
        <ul>
          <li><a href="final_motherhood.html">Motherhood</a></li>
          <li><a href="final_fitness.html">Fitness</a></li>
          <li><a href="final_marriage.html">Marriage and Family</a></li>
        </ul>
      </li>
      <!--END HOVER MENU OPTIONS-->
      <li><a href="final_contact.html">Contact</a></li>   
    </ul>
    <!--END DROPDOWN MENU-->
  </div>
  <!--END MENU DIV-->
</div>
<!--END TITLE DIV-->

<!--IMG AND CONTENT CONTAINER-->
<div id="img_and_content" class="container-fluid">
  <!--ROW DIV-->
  <div class="row">
    <!--TITLE IMG DIV-->
    <div id="title_img_container" class="col-md-6">
      <img id="title_img" src="./img/title_img.jpg">
    </div>
    <!--END TITLE IMG DIV-->

    <!--MAIN PAGE DIV-->
    <div id="main_page_container" class="col-md-6">
      <h1 id="content_title">Insert Title Here . . .</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget viverra ipsum. 
    </div>
    <!--END MAIN PAGE DIV-->
  </div>
  <!--END ROW DIV-->
</div>
<!--END IMG AND CONTENT CONTAINER-->


<!--FOOTER DIV-->
<div id="footer_container">
  &copy; 2018 Erika Marie McBride | Site by Kelly Catbagan
</div>
<!--FOOTER DIV-->

EN

回答 1

Stack Overflow用户

发布于 2018-07-27 05:01:18

您可以添加以下内容到您的CSS来处理博客的子菜单,真正简单的修复时,不在移动版本。我相信也有其他的方法来处理这个问题。在移动视图中,默认情况下会显示您的子菜单。你可能想通过点击事件来切换在移动设备中的显示/隐藏?

@media (min-width: 480px) {
  li.blogs ul {
    position: absolute;
    float: left;
    z-index: 1000;
  }
}

另外,仅供参考,您在自己的超文本标记语言中不止一次包含了jQuery库源,这是不必要的。对于这个示例代码片段,我还删除了对本地JS和IMG文件的引用。

//dropdown hover code
$(document).ready(function() {

  //hover menu bar list
  $('li').hover(function() {
    //find children of list items
    //add stop function to stop hover queue
    $(this).find('ul>li').stop().fadeToggle(300);
  });
  //end dropdown hover

  //menu background color hover
  $('a').css('background-color', 'white').hover(function() {
      $(this).css('background-color', 'pink');
    },
    function() {
      $(this).css('background-color', 'white');
    });
  //end background color hover

});
//end ready function
@media (max-width: 480px) {
  #dropdown_menu li {
    display: block !important;
    border: 1px solid black !important;
    border-right: none !important;
    border-left: none !important;
  }
  #title_img_container {
    margin-bottom: 10%;
  }
  #title_container {
    margin-top: 0 !important;
    margin-bottom: 10%;
    position: relative !important;
  }
  #dropdown_menu li {
    border-bottom: none !important;
  }
}


/*Change size of title div at 650px*/

@media (max-width: 650px) {
  #title_container {
    font-size: 3em !important;
  }
}


/*Change size of title img at 807px*/

@media (min-width: 807px) {
  #title_img_container {
    margin-top: 3%;
  }
  #title_img {
    max-width: 650px;
    height: auto;
  }
}


/*Change size of title img at 1300px*/

@media (min-width: 1300px) {
  #title_img {
    max-width: 600px;
    max-height: 700px;
  }
}

body {
  /*background-color: #dee1e5;*/
  background-image: url("./../img/title_chevron.jpg");
  letter-spacing: 1px;
}

#title_img_container {
  max-width: 100%;
  height: auto;
}

#title_img {
  border: solid 1px black;
  width: 100%;
  height: auto;
  box-shadow: 2px 2px 2px 5px #888888;
}

#title_container {
  border: solid 2px black;
  width: 100%;
  font-family: 'Lobster', cursive;
  font-size: 5em;
  color: gold;
  text-align: center;
  background-color: white;
}

#menu_container {
  font-size: 30px;
}

#img_and_content {
  margin: 5%;
}

#dropdown_menu,
#dropdown_menu ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

#dropdown_menu li {
  display: inline-block;
}

#dropdown_menu a {
  padding: 10px 20px;
  display: block;
  color: inherit;
  text-decoration: none;
}

#dropdown_menu ul li {
  display: none;
  background-color: #afdbcd !important;
  vertical-align: top;
}

li a {
  color: gold;
}

#main_page_container {
  padding: 2% 5% 2% 5%;
  border: black 1px solid;
  font-family: 'Helvetica', Arial;
  background-color: white;
}

#content_title {
  font-family: 'Lobster', cursive;
}

#footer_container {
  color: black;
  text-align: center;
}

@media (min-width: 480px) {
  li.blogs ul {
    position: absolute;
    float: left;
    z-index: 1000;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">

  <!--RESPONSIVE SETUP-->
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  <!--FONTS-->
  <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet">

  <title>YOUR NAME</title>
</head>

<body>
  <!--TITLE DIV-->
  <div id="title_container">YOUR NAME
    <!--MENU DIV-->
    <div id="menu_container">
      <!--DROPDOWN MENU-->
      <ul id="dropdown_menu">
        <li><a href="#">Home</a></li>
        <li><a href="final_about.html">About</a></li>
        <!--HOVER MENU OPTIONS-->
        <li class="blogs"><a href="#">Blogs</a>
          <ul>
            <li><a href="final_motherhood.html">Motherhood</a></li>
            <li><a href="final_fitness.html">Fitness</a></li>
            <li><a href="final_marriage.html">Marriage and Family</a></li>
          </ul>
        </li>
        <!--END HOVER MENU OPTIONS-->
        <li><a href="final_contact.html">Contact</a></li>
      </ul>
      <!--END DROPDOWN MENU-->
    </div>
    <!--END MENU DIV-->
  </div>
  <!--END TITLE DIV-->

  <!--IMG AND CONTENT CONTAINER-->
  <div id="img_and_content" class="container-fluid">
    <!--ROW DIV-->
    <div class="row">
      <!--TITLE IMG DIV-->
      <div id="title_img_container" class="col-md-6">
      </div>
      <!--END TITLE IMG DIV-->

      <!--MAIN PAGE DIV-->
      <div id="main_page_container" class="col-md-6">
        <h1 id="content_title">Insert Title Here . . .</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget viverra ipsum.
      </div>
      <!--END MAIN PAGE DIV-->
    </div>
    <!--END ROW DIV-->
  </div>
  <!--END IMG AND CONTENT CONTAINER-->


  <!--FOOTER DIV-->
  <div id="footer_container">
    &copy; 2018 Erika Marie McBride | Site by Kelly Catbagan
  </div>
  <!--FOOTER DIV-->


</body>

</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51547354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档