如何仅在特定屏幕尺寸之前和之后运行此 jquery 代码?
我有这个 jQuery 代码
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$( document ).ready( function() {
var $body = $('body'); //Cache this for performance
var setBodyScale = function() {
var scaleFactor = 0.35,
scaleSource = $body.width(),
maxScale = 600,
minScale = 30;
var fontSize = scaleSource * scaleFactor; //Multiply the width of the body by the scaling factor:
if (fontSize > maxScale) fontSize = maxScale;
if (fontSize < minScale) fontSize = minScale; //Enforce the minimum and maximums
$('body').css('font-size', fontSize + '%');
}
$(window).resize(function(){
setBodyScale();
});
//Fire it when the page first loads:
setBodyScale();
});
</script>
,但我只想激活这个 jQuery 代码,前提是 @media only screen 并且 (min-width : 1025px) 和 (max-width : 2048px)
不在之后也不在之前。
在 1024px 之前和 2048px 之后,脚本不应执行任何操作。
I have this jQuery code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$( document ).ready( function() {
var $body = $('body'); //Cache this for performance
var setBodyScale = function() {
var scaleFactor = 0.35,
scaleSource = $body.width(),
maxScale = 600,
minScale = 30;
var fontSize = scaleSource * scaleFactor; //Multiply the width of the body by the scaling factor:
if (fontSize > maxScale) fontSize = maxScale;
if (fontSize < minScale) fontSize = minScale; //Enforce the minimum and maximums
$('body').css('font-size', fontSize + '%');
}
$(window).resize(function(){
setBodyScale();
});
//Fire it when the page first loads:
setBodyScale();
});
</script>
But I want to activate this jQuery code only if @media only screen and (min-width : 1025px) and (max-width : 2048px)
not after that and not before that.
before 1024px and after 2048px the script should not do anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为不可能从 javascript 获取 @media 配置。
您可以获取文档的宽度并检查其值:
我不知道是否需要,但如果用户调整窗口大小,您可能还需要绑定到调整大小事件:
d。
I don't think it is possible to get the @media configuration from javascript.
What you can is obtain the document's width and check its value:
I don't know if it required, but if the user resize the window, you might also need to bind to the resize event:
d.
使用 jquery resize 事件:
我认为你可以用它做你想做的事
Use the jquery resize event :
I think you can do what you want with this