想做个自适应宽度导航栏...简化的代码如下,大佬们帮我看看哪里出了问题var navbar_Reload = function(){
if(document_Width>= 1080)
{
$(".navbottom li").mouseover(function(){
alert(document_Width);
}
}
$(window).resize(function(){
document_Width = document.body.clientWidth;
navbar_Reload();
});问题就在于,当窗口小于1080时,鼠标移上去同样会alert,但是把alert这一行放在mouseover外面就不会,这是什么问题呢,是我哪里错了嘛……我的基础不是很牢...谢谢大家了
if(document_Width>= 1080)
{
$(".navbottom li").mouseover(function(){
alert(document_Width);
}
}
$(window).resize(function(){
document_Width = document.body.clientWidth;
navbar_Reload();
});问题就在于,当窗口小于1080时,鼠标移上去同样会alert,但是把alert这一行放在mouseover外面就不会,这是什么问题呢,是我哪里错了嘛……我的基础不是很牢...谢谢大家了
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</style>
</head>
<body><ul class="navbottom">
<li>xxxxxxxxxxxxx</li>
</ul><script type="text/javascript">
var navbar_Reload = function(w){
$(".navbottom li").off("mouseover");
if(w >= 1080)
{
$(".navbottom li").mouseover(function(){
alert(w);
});
}
}
function onre(){
var document_Width = document.body.clientWidth;
navbar_Reload(document_Width);
}
$(window).resize(onre);
$(window).ready(onre);
</script>
</body>
</html>
如果想让alert只在document_Width大于等于1080时触发,要么像一楼那样不断的移除、添加,要么在mouseover中做下判断。$(".navbottom li").mouseover(function(){
if(document_Width>= 1080){
alert(document_Width);
}
});
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</style>
</head>
<body><ul class="navbottom">
<li>xxxxxxxxxxxxx</li>
</ul><script type="text/javascript">
var document_Width;
var navbar_Reload = function(){
$(".navbottom li").mouseover(function(){
if (document_Width < 1080) return;
console.log("mouseover",document_Width);
});
$(".navbottom li").mouseleave(function(){
if (document_Width < 1080) return;
console.log("mouseleave",document_Width);
});
}
function onre(){
document_Width = document.body.clientWidth;
}
$(window).resize(onre);
$(window).ready(function () {
navbar_Reload();
onre();
});
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</style>
</head>
<body><ul class="navbottom">
<li>xxxxxxxxxxxxx</li>
</ul><script type="text/javascript">
var document_Width;
var navbar_Reload = function(){
$(".navbottom li").mouseover(function(){
if (document_Width < 1080) return;
console.log("mouseover",document_Width);
});
$(".navbottom li").mouseleave(function(){
if (document_Width < 1080) return;
console.log("mouseleave",document_Width);
});
}
function onre(){
document_Width = document.body.clientWidth;
}
$(window).resize(onre);
$(window).ready(function () {
navbar_Reload();
onre();
});
</script>
</body>
</html>
我又试了一下,还是不起作用,即使在里面加判断条件之后还是不受控制,窗口宽度改变之后只有刷新能恢复正常...可是刷新的话太影响体验了
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</style>
</head>
<body><ul class="navbottom">
<li>xxxxxxxxxxxxx</li>
</ul><script type="text/javascript">
$(window).ready(function () {
$(".navbottom li").mouseover(function(){
if (document.body.clientWidth < 1080) return;
console.log("mouseover");
});
$(".navbottom li").mouseleave(function(){
if (document.body.clientWidth < 1080) return;
console.log("mouseleave");
});
});
</script>
</body>
</html>