admin管理员组文章数量:1313284
I want to reset a particular element once the browser window has been resized from any size less than 641px, to a greater size. Here is an example of what I am trying to achieve, written in pseudo code:
if (browser.window <= 641px && browser.window.resizedTo > 641px) {
$( ".foo" ).removeClass( "bar" )
}
Thank you!
I want to reset a particular element once the browser window has been resized from any size less than 641px, to a greater size. Here is an example of what I am trying to achieve, written in pseudo code:
if (browser.window <= 641px && browser.window.resizedTo > 641px) {
$( ".foo" ).removeClass( "bar" )
}
Thank you!
Share Improve this question asked Mar 3, 2016 at 19:12 Ralph David AbernathyRalph David Abernathy 5,51813 gold badges56 silver badges87 bronze badges2 Answers
Reset to default 7I'll assume for the sake of this question that you mean 641px as the width, but I left the innerHeight variable there, too, in case you need it:
// Closure last-height/width
var lastX = window.innerWidth
var lastY = window.innerHeight
function fooOnResize() {
var x = window.innerWidth
var y = window.innerHeight
if (lastX <= 641 && 641 < x) {
$(".foo").removeClass("bar")
}
lastX = x
lastY = y
}
window.addEventListener("resize", fooOnResize)
// Also okay: window.onresize = fooOnResize
The trick is to basically hold the last size in some closure variables, then on resize, you can make your parison. After you have done the parison and the work, you store the current x/y as the last ones, for the next resize.
Try the window.onresize
event:
https://developer.mozilla/en-US/docs/Web/API/GlobalEventHandlers/onresize
You can get the current dimensions of the browser window using window.innerHeight
and window.innerWidth
:
window.onresize = function() {
console.log('height: ' + window.innerHeight);
console.log('width: ' + window.innerWidth);
}
版权声明:本文标题:javascript - Detect when browser window has been resized from certain size to a larger size? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741948480a2406566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论