Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

jquery - How to set viewport dynamically?

If I want to change the viewport tag dynamically, what do I need to do?

For Mobile

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" /> 

iPad (width over 768px)

<meta name="viewport" content="width=device-width, initial-scale=0.7, maximum-scale=2, user-scalable=yes" /> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For sample :

<meta name="viewport" content="width = 384" id="myViewport">

It sets the layout viewport width to 384 pixels. This works in most modern browsers; Nokia WebKit being the prime exception.

You might want to give the layout viewport a width of 380px unless you’re on a widescreen (read tablet as you expect) device, in which case you might want to double it to 768. Or whatever.

<meta id="myViewport" name="viewport" content="width = 384">
<script>
if (screen.width > 768) {
    var mvp = document.getElementById('myViewport');
    mvp.setAttribute('content','width=768');
}
</script>

This script is executed automatically and changes the meta viewport tag directly.

It’s also possible to force a change much later, after the page has been downloaded and rendered:

<meta id="myViewport" name="viewport" content="width = 384">
<script>
window.onload = function () {
    if (screen.width > 768) {
        var mvp = document.getElementById('myViewport');
        mvp.setAttribute('content','width=768');
    }
}
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...