所以基本上我有一个按钮,当用户点击它时,一个div淡入。我希望当用户点击div上的任何地方时它会淡出。 (div占据了整个屏幕)
这是我到目前为止所做的,当你点击"关于"它基本上只是在div中消失。按钮。每当我点击屏幕上的任何地方时,我都需要它淡出。
$(function() {
$('#fadeContent').click(function(){
$('#bodyContent').fadeIn(500);
});
});

body {
background-color: black;
font-family: "Source Sans Pro", sans-serif;
color: #ccc;
z-index: -100;
background-color:black;
}
#bodyContent{
display:none;
position:absolute;
top:0; left;0;
width:100%; height:100%;
background:#454545;
z-index: 100;
}
#home {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 20%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
.about {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 37%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#projects {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 54%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#contact {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 71%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#home:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#about:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#projects:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#contact:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
@keyframes fadein {
from {
opacity:0;
}
to {
opacity:1;
}
}
@-webkit-keyframes fadein { /* Safari and Chrome */
from {
opacity:0;
}
to {
opacity:1;
}
}

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home</title>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro|Play" rel="stylesheet">
<link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css'>
<link rel='stylesheet prefetch' href='http://rawgit.com/fralec/elegantShareJS/master/css/elegant.css'>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<button id="home">Home</button>
<button id="fadeContent" class="about">About</button>
<button id="projects">Projects</button>
<button id="contact">Contact</button>
<div id="bodyContent">
<h1>Page</h1>
<p>Content</p>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
<script type="text/javascript" src="js/elegant.js"></script>
<script type="text/javascript" src="js/transition.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</body>
</html>
&#13;
答案 0 :(得分:0)
为刚刚淡入的正文内容添加一个点击监听器,然后将其淡出!
$(function() {
$('#fadeContent').click(function(){
var bodyContent = $('#bodyContent')
bodyContent.fadeIn(500);
bodyContent.click(function() {
$(this).fadeOut(500);
})
});
});
&#13;
body {
background-color: black;
font-family: "Source Sans Pro", sans-serif;
color: #ccc;
z-index: -100;
background-color:black;
}
#bodyContent{
display:none;
position:absolute;
top:0; left;0;
width:100%; height:100%;
background:#454545;
z-index: 100;
}
#home {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 20%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
.about {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 37%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#projects {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 54%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#contact {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 71%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#home:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#about:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#projects:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#contact:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
@keyframes fadein {
from {
opacity:0;
}
to {
opacity:1;
}
}
@-webkit-keyframes fadein { /* Safari and Chrome */
from {
opacity:0;
}
to {
opacity:1;
}
}
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home</title>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro|Play" rel="stylesheet">
<link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css'>
<link rel='stylesheet prefetch' href='http://rawgit.com/fralec/elegantShareJS/master/css/elegant.css'>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<button id="home">Home</button>
<button id="fadeContent" class="about">About</button>
<button id="projects">Projects</button>
<button id="contact">Contact</button>
<div id="bodyContent">
<h1>Page</h1>
<p>Content</p>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
<script type="text/javascript" src="js/elegant.js"></script>
<script type="text/javascript" src="js/transition.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</body>
</html>
&#13;
答案 1 :(得分:0)
当您致电fadeOut
时,只需将fadeIn
连接到点击事件中。
$('#fadeContent').click(function(){
$('#bodyContent').click(function() {
$(this).fadeOut(500);
}).fadeIn(500);
});
修改强>
上面的代码将在每次单击按钮时保持连接事件处理程序。假设所有按钮都将加载并显示内容并单击内容应始终返回按钮考虑;
var $content = $('#bodyContent');
$('body').on('click','button', function() {
$content.fadeIn(500);
});
$content.on('click', function() {
$content.fadeOut(500);
});
然后,您可以为各个按钮附加点击事件,以便检索并将特定内容分配到#bodyContent
,而无需挂钩fadeIn / fadeOut。