你好,编码不是很精通。当#background按钮单击时,我会#content淡出。如何在按钮上显示文本以显示隐藏内容/显示内容?
当隐藏内容点击时,我还需要淡出另外两个div(.button-next,.button-prev)?
任何有帮助的人?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var openingdelay = 200; // Delay before fade in initiated (default is 200)
var closingdelay = 100; // Delay before fade out initiated (default is 100)
var openingspeed = 25; // Speed of fade in (default is 25)
var closingspeed = 15; // Speed of fade out (default is 15)
function Show(elementid) {
ele = document.getElementById(elementid);
if(ele.style.display == 'none') {
ele.style.opacity = 0;
ele.style.filter = 'alpha(opacity=0)';
ele.style.display = '';
valueop = 1;
setTimeout("fadeIn()", openingdelay);
}
else {
valueop = 9;
setTimeout("fadeOut()", closingdelay);
}
}
function fadeOut() {
if(valueop < 1) {
ele.style.display = 'none';
return false;
}
ele.style.opacity = valueop/10;
ele.style.filter = 'alpha(opacity='+(valueop*10)+')';
valueop = valueop - 1;
setTimeout("fadeOut()", closingspeed);
}
function fadeIn() {
if(valueop > 10) {
return false;
}
ele.style.opacity = valueop/10;
ele.style.filter = 'alpha(opacity='+(valueop*10)+')';
valueop = valueop + 1;
setTimeout("fadeIn()", openingspeed);
}
</script>
<style type="text/css">
.layer1 {
margin: 0;
padding: 0;
width: 500px;
}
.background-button {
margin: 1px;
color: #fff;
padding: 3px 10px;
cursor: pointer;
position: relative;
background-color:#c30;
float:right;
}
#content {
padding: 5px 10px;
background-color:#000;
float:left; width:400px; height:600px; color:#FFF
}
</style>
</head>
<body>
<div id="content" >Hello! This will remain hidden until the link is clicked.</div>
<div class="background-button"><a href="#" onClick="Show('content'); return false;">Click Me To Reveal More</a></div>
<div style="background-color:#000; color:#FFF; position:absolute; right:0px; top:100px; height:50px; width:100px;" id="button-next">next</div>
<div style="background-color:#000; color:#FFF; position:absolute; right:0px; top:200px; height:50px; width:100px;" id="button-prev">prev</div>
</body>
</html>
答案 0 :(得分:0)
我会使用注册而不是内联事件处理程序添加事件侦听器:
var button = document.querySelector('.background-button');
button.addEventListener("click", function(){
Show('content', this.children[0]);
return false;
});
函数Show
必须相应地修改为:
function Show(elementid, link) {
ele = document.getElementById(elementid);
if (ele.style.display == 'none') {
ele.style.opacity = 0;
ele.style.filter = 'alpha(opacity=0)';
ele.style.display = '';
valueop = 1;
setTimeout("fadeIn()", openingdelay);
}
else {
valueop = 9;
setTimeout("fadeOut()", closingdelay);
}
if(link.getAttribute('data-hidden') == "hidden"){
link.innerHTML = "Click Me To Hide Stuff";
link.removeAttribute("data-hidden");
}else{
link.innerHTML = "Click Me To Reveal More";
link.setAttribute("data-hidden", "hidden");
}
}
答案 1 :(得分:0)
我修改了您的html以将ID和ID添加到链接:#show_more
HTML:
<div id="content" >Hello! This will remain hidden until the link is clicked.</div>
<div class="background-button"><a id="show_more" href="#">Click Me To Reveal More</a></div>
<div style="background-color:#000; color:#FFF; position:absolute; right:0px; top:100px; height:50px; width:100px;" id="button-next">next</div>
<div style="background-color:#000; color:#FFF; position:absolute; right:0px; top:200px; height:50px; width:100px;" id="button-prev">prev</div>
jQuery的:
$('#content, #button-next, #button-prev').hide();
$('#show_more').click(function(e) {
e.preventDefault(); // disable default <a> behavior
if ($('#content').is(':visible')) {
$('#show_more').text('Show Content');
} else {
$('#show_more').text('Hide Content');
}
$('#content, #button-next, #button-prev').fadeToggle();
});
第一行只是隐藏内容和按钮。你应该通过CSS或内联样式隐藏它们并删除这行代码。
编辑:jsFiddle