使用JQuery

时间:2017-04-16 22:25:36

标签: javascript jquery html css

我正在尝试将href按钮和菜单定位为在页面加载时淡入。我在查找确切的代码时遇到了麻烦,因为它放在了很多不同的标签里面,而且我对JS和JQuery还不熟悉。我已经包含了所有代码的小提琴。

到目前为止,我已经管理了这段代码:

HTML:

<div id="ImageMain">

<ul id="nav">
    <li id="navm"><a href="#">Men</a></li>
    <li id="navm"><a href="#">Women</a></li>
    <li id="navm"><a href="#">Contact</a></li>
</ul>

<a href="#" id="homeb">Shop Now</a>

</div>

<script>    
$(document).ready(function() {
    $("#navm").fadeIn(500);
});
$(document).ready(function() {
    $("#homeb").fadeIn(500);
});
</script>

CSS:

#ImageMain {
background-image: url(http://juliapavel.de/wp-content/uploads/2016/07/DSC1139-1.jpg);
background-repeat: no-repeat;
background-size: cover;
height: 1400px;
text-align: center;
color: black;
margin-right: auto;
margin-left: auto;
padding-top: 0px;
position: relative;
}

#ImageMain h1 {
padding-bottom: 10px;
padding-top: 450px;
}

#homeinfo {
padding-bottom: 15px;
}

#brand {
float: left;
padding: 100px;
}

#Pronto {
width: 150px;
}

#ImageMain #brand a {
font-size: 20px;
transition-duration: 1s;
}

#nav #navm a {
transition-duration: 1s;
}

#brand a:hover {
opacity: .3;
transition-duration: 1s;
}

#navm a:hover {
opacity: .3;
transition-duration: 1s;
}

#navm {
margin-right: auto;
margin-left: auto;
display: none;
}

#homeb {
text-decoration: none;
color: black;
border-radius: 5px;
border: solid 1px black;
padding: 12px;
transition-duration: 1s;
}

#wrapper a {
text-decoration: none;
border: solid 1px #141414;
border-radius: 5px;
padding: 10px;
color: #141414;
}

#homeb:hover {
opacity: .3;
transition-duration: 1s;
}

#wrapper a:hover {
opacity: 0.7;
transition-duration: 0.8s;
}

h3 {
color: white;
}

#nav {
list-style: none;
padding: 0;
padding-top: 5px;
text-align: center;
}

#nav li {
display: inline-block;
padding: 7px;
}

#nav a {
text-decoration: none;
color: black;
font-size: 15px;
}

https://jsfiddle.net/tux41Lvp/

2 个答案:

答案 0 :(得分:1)

您在展示项目之前忘记隐藏这些项目,并且您不能拥有两个具有相同ID的单独项目,因此我更改了选择器。

$("#nav a, #homeb").hide();
$(document).ready(function() {
    $("#nav a").fadeIn(1000);
    $("#homeb").fadeIn(1000);
});
#ImageMain {
background-image: url(http://juliapavel.de/wp-content/uploads/2016/07/DSC1139-1.jpg);
background-repeat: no-repeat;
background-size: cover;
height: 1400px;
text-align: center;
color: black;
margin-right: auto;
margin-left: auto;
padding-top: 0px;
position: relative;
}

#ImageMain h1 {
padding-bottom: 10px;
padding-top: 450px;
}

#homeinfo {
padding-bottom: 15px;
}

#brand {
float: left;
padding: 100px;
}

#Pronto {
width: 150px;
}

#ImageMain #brand a {
font-size: 20px;
transition-duration: 1s;
}

#nav #navm a {
transition-duration: 1s;
}

#brand a:hover {
opacity: .3;
transition-duration: 1s;
}

#navm a:hover {
opacity: .3;
transition-duration: 1s;
}

#navm {
margin-right: auto;
margin-left: auto;
display: none;
}

#homeb {
text-decoration: none;
color: black;
border-radius: 5px;
border: solid 1px black;
padding: 12px;
transition-duration: 1s;
}

#wrapper a {
text-decoration: none;
border: solid 1px #141414;
border-radius: 5px;
padding: 10px;
color: #141414;
}

#homeb:hover {
opacity: .3;
transition-duration: 1s;
}

#wrapper a:hover {
opacity: 0.7;
transition-duration: 0.8s;
}

h3 {
color: white;
}

#nav {
list-style: none;
padding: 0;
padding-top: 5px;
text-align: center;
}

#nav li {
display: inline-block;
padding: 7px;
}

#nav a {
text-decoration: none;
color: black;
font-size: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ImageMain">

<ul id="nav">
    <li><a href="#">Men</a></li>
    <li><a href="#">Women</a></li>
    <li><a href="#">Contact</a></li>
</ul>

<a href="#" id="homeb">Shop Now</a>

</div>

答案 1 :(得分:1)

您需要为按钮锚点创建父div:

<div id="link">
    <a href="#" id="homeb">Shop Now</a>
</div>

您需要隐藏目标元素(您必须以父母为目标):

#nav{ display:none;}
#link{ display:none;}   

查看工作片段中的js

$(document).ready(function() {
            $("#nav").fadeIn(500);
            $("#link").fadeIn(1000);
        });
#ImageMain {
	background-image: url(http://juliapavel.de/wp-content/uploads/2016/07/DSC1139-1.jpg);
	background-repeat: no-repeat;
	background-size: cover;
	height: 1400px;
	text-align: center;
	color: black;
	margin-right: auto;
	margin-left: auto;
	padding-top: 0px;
	position: relative;
}

#ImageMain h1 {
	padding-bottom: 10px;
	padding-top: 450px;
}

#homeinfo {
	padding-bottom: 15px;
}

#brand {
	float: left;
	padding: 100px;
}

#Pronto {
	width: 150px;
}

#ImageMain #brand a {
	font-size: 20px;
	transition-duration: 1s;
}

#nav #navm a {
	transition-duration: 1s;
}

#brand a:hover {
	opacity: .3;
	transition-duration: 1s;
}

#navm a:hover {
	opacity: .3;
	transition-duration: 1s;
}

#navm {
	margin-right: auto;
	margin-left: auto;
	display: none;
}

#homeb {
	text-decoration: none;
	color: black;
	border-radius: 5px;
	border: solid 1px black;
	padding: 12px;
	transition-duration: 1s;
}

#wrapper a {
	text-decoration: none;
	border: solid 1px #141414;
	border-radius: 5px;
	padding: 10px;
	color: #141414;
}

#homeb:hover {
	opacity: .3;
	transition-duration: 1s;
}

#wrapper a:hover {
	opacity: 0.7;
	transition-duration: 0.8s;
}

h3 {
	color: white;
}

#nav {
 	list-style: none;
 	padding: 0;
 	padding-top: 5px;
 	text-align: center;
}

#nav li {
 	display: inline-block;
	padding: 7px;
}

#nav a {
	text-decoration: none;
	color: black;
	font-size: 15px;
}

#nav{ display:none;}
#link{ display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ImageMain">
	
	<ul id="nav">
		<li id="navm"><a href="#">Men</a></li>
		<li id="navm"><a href="#">Women</a></li>
		<li id="navm"><a href="#">Contact</a></li>
	</ul>
	
  <div id="link">
    <a href="#" id="homeb">Shop Now</a>
  </div>
	

</div>