【发布时间】:2014-06-05 00:42:41
【问题描述】:
这里有很多类似的问题,但没有一个能真正解决我的需要。
这是一个带有问题小教程的 jsfiddle 演示: http://jsfiddle.net/V482z/2/(或全屏结果http://jsfiddle.net/V482z/2/embedded/result/)
如上述教程中所述,我已成功创建jquery 脚本以在屏幕较小时显示/隐藏侧边栏内容,但它不是动态的。这是一个非常罕见的场景,普通用户会以这种方式玩他的浏览器屏幕大小,但无论如何,我想解决这个难题。
如何取消侧边栏div的jquery效果?
谢谢!
对于那些不喜欢 jsfiddle 并希望将这个示例复制到您计算机上的纯 html 文件中的人,这里是完整的代码:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<title>jquery show/hide div test</title>
<style>
#showhide{
clear: both;
float: left;
display: none;
height: 25px;
cursor: pointer;
background-color: #F00;
}
@media only screen and (max-width:499px) {
#showhide{
display: block;
}
}
#container {
clear: both;
display: block;
width: 100%;
}
#sidebar {
clear: both;
float: left;
display: block;
width: 240px;
height: 450px;
background-color: #777;
}
@media only screen and (max-width:499px) {
#sidebar {
display: none;
}
}
#content {
display: block;
width: 500px;
height: 450px;
background-color: #CCC;
}
</style>
</head>
<body>
<div id="showhide">
show/hide sidebar
</div>
<div id="container">
<div id="sidebar">#1 automatically show/hide sidebar content when screen IS or GETS resized/smaller then 500px, but leave user the option to show hidden content on demand.
<br/>
<br/>tutorial:
<br/>1. use the JSFIDDLE screen divider line to resize this content and observe the effect when it gets smaller than 500px
<br/>2. now click on red show/hide sidebar block
<br/>3. now click again to hide sidebar content
<br/>4. now resize back the window screen to larger than 500px size.
<br/>5. Where is the sidebar? Why is it not restored back?
</div>
<div id="content">
#2 content is always visible
<br/>
<br/>
<b>THE ISSUE:</b> as you can see, when you toggle sidebar in less than 500px screen mode and resize the window back to original larger size, sidebar is still toggled/hidden due jquery effect.
<br/>
<br/>
How to restore jquery hiding of the sidebar div?
<br/>
<br/>
Thanks!
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#showhide").click(function () {
$("#sidebar").fadeToggle("slow");
});
});
</script>
</body>
</html>
【问题讨论】:
标签: javascript jquery html css show-hide