admin管理员组文章数量:1414886
I have a function that tells what the current width of the user's screen is but when i resize window to width less than 1024 that function doesnt output width less than 1024
function xxx(){
var x = window.screen.Width
console.log(x)
}
setInterval(xxx, 1000)
output:
1024
even if screen width less than 1024 how can i fix this without using @media requests?
already tried:
window.screen.innerwidth
I have a function that tells what the current width of the user's screen is but when i resize window to width less than 1024 that function doesnt output width less than 1024
function xxx(){
var x = window.screen.Width
console.log(x)
}
setInterval(xxx, 1000)
output:
1024
even if screen width less than 1024 how can i fix this without using @media requests?
already tried:
window.screen.innerwidth
- 2 it was so hard to paste the exact actual code you used? there are so many mistakes in just 5 lines – Diego D Commented May 27, 2022 at 7:53
- screen width !== window width. The answers to this question cover basically every different approach to retrieving screen/window sizes. – DBS Commented May 27, 2022 at 9:12
2 Answers
Reset to default 5Like @YoussoufOumar said instead of using setInterval
to get the user's screen width, use resize
event to get the screen width whenever the screen resolution changes.
window.screen.width
get only the user's screen width and it doesn't change according to the manual resizing of the browser
Instead window.innerWidth
gets the browser width and it changes according to the manual resizing of the browser
function widthResizer(){
var width = window.innerWidth
console.log(width)
}
// Getting the width of the browser on load
widthResizer()
// Getting the width of the browser whenever the screen resolution changes.
window.addEventListener('resize', widthResizer)
Instead of setting up a setIntervall
to track screen width changes, use the built in resize
event.
As @Kumara pointed it, you would wanna use
window.innerWidth
, instead ofwindow.screen.width
, as the last one give the screen's original size, not the resized one.
function xxx(){
var x = window.innerWidth
console.log(x)
}
// that gives you the width on load
xxx();
// that is for when you change the width manually
window.addEventListener('resize', xxx);
本文标签: htmlJavascripttrack window screen widthStack Overflow
版权声明:本文标题:html - JavaScript, track window screen width - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745174825a2646184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论