调用#DIV上的函数,而不是window()
Calling function on #DIV rather than window()
我写了这个脚本,它有效地隐藏了一个依赖于浏览器的工具栏window().width。我后来意识到,由于我想计算宽度的方式……:
'$csspagewidth2=$Cspagewidth*2;'
我可以用#DIV.width来代替window().width。但当我试图将window(.width)更改为#main content时。它无法工作。有人能帮忙吗?
if ($("#main-content").width() < 600) {
$( "#toolbarright").hide();
}
else {
$("#toolbarright").show();
}
$("#main-content").resize(function() {
windowWidth = $(this).width();//you need to use this for changable values
if ( windowWidth < 600) {
$( "#toolbarright").hide();
} else if ( windowWidth > 600) {
$("#toolbarright").show();
}
});
小提琴:http://jsfiddle.net/ablueman/vdgeLsgL/16/
上下文:https://www.ablueman.co.uk/testbench/layout.php
$(main-content)
将获取main
变量(undefined
),并从中减去content
变量(也称为undefined
),从而获得NaN
,然后该变量将用于尝试实例化jQuery
对象。它在DOM中找不到任何匹配的元素。
您想要$("#main-content")
使用CSS很容易
@media only screen and (max-width: 320px){
#toolbarright { display:none; }
}
DEMO1
更新1:
JQuery
$(window).on('resize', function(){
$('#main-content').each(function(){
var box = $(this);
var width = box.width();
var height = box.height();
if ( width < 600) {
box.find( "#toolbarright").hide();
} else if ( width > 600) {
box.find("#toolbarright").show();
}
$('.size').html(width+'x'+height+' (r: '+(width/height).toFixed(3)+')');
});
}).trigger('resize');
CSS
#main-content
{
margin: 120px;
position: relative;
}
#toolbarright
{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow:hidden;
outline: 1px solid grey;
height:100px;
}
DEMO2
想好了:
$( document ).ready(function() {
if ($("#maincontent").width() < 600){
$( "#toolbarright").hide();
} else { $("#toolbarright").show();}
});
$(window).resize(function() {
if ($("#maincontent").width() < 600){
$( "#toolbarright").hide();
} else { $("#toolbarright").show();}
});
http://jsfiddle.net/ablueman/vdgeLsgL/
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- 直接下载文件,而不是从window.open(url)
- 添加文字和评论功能更新Div
- $window.ga在AngularJS事件中未定义
- 如何将PDF作为二进制文件传递到window.open()
- 点击后隐藏潜水?(但如果Div是一面旗帜呢?)
- 使用JavaScript动态插入DIV的成本有多高
- window.onload没有'无法在Android WebView中工作
- DIV并排,位置不正确
- Div根据<选择>菜单
- window.location替换并传递URL历史记录条目中的变量
- window.on.scroll事件未启动
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- Href:当前DIV中的目标ID
- 将 DIV 从名为 A 的文件添加到使用 window.open() 从 grails 中名为 B 的文件打开的新窗口中
- Window.Print() Div with Overflow-x?
- 如何使用window.print()打印具有水平和垂直滚动条的DIV的全部内容
- 观察事件'Window Onload'在刷新的DIV元素内
- 调用#DIV上的函数,而不是window()
- 谷歌铬-如何限制Javascript's window.查找特定DIV