基于窗口宽度jquery的函数的替代方法是什么
what is the alternate way to do a function based on window width jquery?
当宽度<450,如果>450,切换方式,隐藏按钮并显示内容。用css代码隐藏和显示没有问题,但问题是一旦我单击<450,东西出现了,如果我回到>450屏幕,按钮将不会再次隐藏。我曾试图在jquery中放入window.resize,它给了我奇怪的性能。点击运行代码查看。非常感谢。
if ($(window).width() < 450) {
var show_stuff=$('#all_vote_menu');
var button_menu=$('#button');
$(button_menu).on('click', function(){
$(show_stuff).show(400,'swing');
$(button_menu).hide();
});
$(document).mouseup(function (e){
if (!show_stuff.is(e.target) // if the target of the click isn't the container...
&& show_stuff.has(e.target).length === 0) // ... nor a descendant of the container
{
$(show_stuff).hide(400,'swing');
$(button_menu).fadeIn();
}
});
}
#button{
display:none ;/*hide when >450px*/
}
.all_vote_menu{
width:200px;
height:200px;
background:yellow;
}
@media screen and (max-width: 450px) {
#button{
display:block ;/*show when <450px*/
color:green;
}
.all_vote_menu{
display:none;/*hide when <450px*/
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="button" name="all_vote_show" value="Menu"/>
<div class="all_vote_menu" id='all_vote_menu'>hello</div>
您不需要if条件来验证窗口宽度,因为只有当窗口小于450px时才能访问按钮。
删除这个,它就工作了。
试试这个演示
jquery代码:
var show_stuff=$('#all_vote_menu');
var button_menu=$('#button');
$(button_menu).on('click', function(){
$(show_stuff).show(400,'swing');
$(button_menu).hide();
});
$(document).mouseup(function (e){
if (!show_stuff.is(e.target) // if the target of the click isn't the container...
&& show_stuff.has(e.target).length === 0) // ... nor a descendant of the container
{
$(show_stuff).hide(400,'swing');
$(button_menu).fadeIn();
}
});
取消宽度检查if ($(window).width() < 450) {
如果正在显示,请在中添加检查
$(document).mouseup(function (e){
if ($(window).width() >= 450 && !show_stuff.is(e.target) ...
相关文章:
- Javascript 对象的推荐构造函数是什么?
- JavaScript 中的“ScriptEngine”函数是什么?
- jQuery中的.proxy()函数是什么
- 删除 href 周围的文本的正确 jquery 函数是什么
- 弹出窗口的确切函数是什么
- javascript中的strord函数是什么?
- 访问cookie最可靠的JavaScript函数是什么?
- Node.js中的函数构造函数是什么
- 交叉过滤中的reduceAdd, reduceSum, reducerremove函数是什么?它们应该如何使用?
- 为什么console.trace()的结果以一个“匿名函数”结束?这个函数是什么?
- 数组字面量中的构造函数是什么?
- Javascript中类似php函数substr()的函数是什么?
- 参数中的匿名函数是什么意思?
- 拆分函数是什么样子的?
- jquery中. max()的等效函数是什么?
- 在Node Js中,tcp连接中socket的.on函数是什么?
- firefox浏览器(所有版本)中的.load()等效函数是什么?
- jQuery' width()函数的Javascript等效函数是什么?
- jQuery中的.selectmany()函数是什么?
- setTimeout执行的函数是什么?