[英]Can't set the meta viewport width back to the screen's width
I am trying to get my script to: 我想让我的脚本:
by clicking on a link (#breakCSSButton), change the meta tag's viewport width to 1280 resizing it. 通过单击链接(#breakCSSButton),将meta标签的视口宽度更改为1280,以调整其大小。
by clicking on the link again, change the meta tag's viewport width back to window.screen.width 通过再次单击链接,将meta标签的视口宽度更改回window.screen.width
I dont understand why its not working. 我不明白为什么它不起作用。 I have an anchor tag button:
我有一个锚标记按钮:
<a href="#" id="breakCSSButton">
Heres the code: 这是代码:
$(function () {
if ( $('#breakCSSButton').hasClass('dviewToggledOn') == false ) {
console.log($('#breakCSSButton').hasClass('dviewToggledOn'));
$('#breakCSSButton').on('click', function () {
viewportBreak = document.querySelector("meta[name=viewport]");
viewportBreak.setAttribute('content', 'user-scalable=1, initial-scale=1, maximum-scale=4, width=1280');
$( window ).resize();
$('#breakCSSButton').toggleClass('dviewToggledOn');
});
}
if ( $('#breakCSSButton').hasClass('dviewToggledOn') == true ) {
$('.dviewToggledOn').on('click', function () {
deviceScreenWidth = window.screen.width;
viewportBreak = document.querySelector("meta[name=viewport]");
viewportBreak.setAttribute('content', "user-scalable=1, width=" + deviceScreenWidth + ", initial-scale=1, maximum-scale=4");
$( window ).resize();
$('#breakCSSButton').toggleClass('dviewToggledOn');
});
}
});
@Whothehellisthat That didn't seem to work... is this what you mean?: @Whothehellisthat似乎没有用...这是您的意思吗?:
$('#breakCSSButton').on('click', function () {
if ( $('#breakCSSButton').hasClass('dviewToggledOn') == false ) {
viewportBreak = document.querySelector("meta[name=viewport]");
viewportBreak.setAttribute('content', 'user-scalable=1, initial-scale=1, maximum-scale=4, width=1280');
$( window ).resize();
$('#breakCSSButton').toggleClass('dviewToggledOn');
}
if ( $('#breakCSSButton').hasClass('dviewToggledOn') == true ) {
deviceScreenWidth = window.screen.width;
viewportBreak = document.querySelector("meta[name=viewport]");
viewportBreak.setAttribute('content', "user-scalable=1, width=" + deviceScreenWidth + ", initial-scale=1, maximum-scale=4");
$( window ).resize();
$('#breakCSSButton').toggleClass('dviewToggledOn');
}
});
Okay the following works. 好的,以下工作。 I decided to do a .show and .hide on two different html buttons.
我决定在两个不同的html按钮上执行.show和.hide。 The code is simpler this way.
这样的代码更简单。 I think the problem is that it didn't like working on the same element (#breakCSSButton) after more than 1 dynamic change (ie adding a class and working on it).
我认为问题在于,在进行了1次以上的动态更改(即添加一个类并对其进行处理)之后,它不喜欢对同一个元素(#breakCSSButton)进行处理。 It may also have been recognizing the click actions on the same class and running both if statements based on a single click...
它还可能已经识别出同一类上的单击动作,并且基于一次单击就同时运行两个if语句...
$(function () {
$('#breakCSSButton').on('click', function () {
viewportBreak = document.querySelector("meta[name=viewport]");
viewportBreak.setAttribute('content', 'user-scalable=1, initial-scale=1, maximum-scale=4, width=1280');
$( window ).resize();
$('#breakCSSButton').hide();
$('#restoreCSSButton').show();
});
});
$(function () {
$('#restoreCSSButton').on('click', function () {
deviceScreenWidth = window.screen.width;
viewportBreak = document.querySelector("meta[name=viewport]");
viewportBreak.setAttribute('content', "user-scalable=1, width=" + deviceScreenWidth + ", initial-scale=1, maximum-scale=4");
$( window ).resize();
$('#breakCSSButton').show();
$('#restoreCSSButton').hide();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.