admin管理员组文章数量:1429064
I was working on a jquery simple modal. I am trying to load it- on-load and after some 15 Sec it will close automatically. So what i cannot achieve is if i am setting a width and height to that modal and if the users browser screen resolution changes([ctrl(+)
]or[ctrl(-)
]) the size of the modal will varies. So i tried to catch the user's screen resolution using screen.width
and screen.height
and assigning that width to that modal. But it is messing up i don't know why. Here is the demo link of what i am trying to do. This is my jquery code i am using.
<script>
$(document).ready(function() {
(function () {
var width = screen.width,
height = screen.height;
setInterval(function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange');
}
}, 50);
}());
$.modal($("#basic-modal-content"));
$("#simplemodal-container").width(screen.width);
$("#simplemodal-container").height(screen.height);
});
setTimeout(function() {
$.modal.close();
}, 15000);
</script>
The actual thing is i am trying to hide my screen and put an ad in the modal until the page loads. Hope my question is clear. Thank you in Advance !!
I was working on a jquery simple modal. I am trying to load it- on-load and after some 15 Sec it will close automatically. So what i cannot achieve is if i am setting a width and height to that modal and if the users browser screen resolution changes([ctrl(+)
]or[ctrl(-)
]) the size of the modal will varies. So i tried to catch the user's screen resolution using screen.width
and screen.height
and assigning that width to that modal. But it is messing up i don't know why. Here is the demo link of what i am trying to do. This is my jquery code i am using.
<script>
$(document).ready(function() {
(function () {
var width = screen.width,
height = screen.height;
setInterval(function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange');
}
}, 50);
}());
$.modal($("#basic-modal-content"));
$("#simplemodal-container").width(screen.width);
$("#simplemodal-container").height(screen.height);
});
setTimeout(function() {
$.modal.close();
}, 15000);
</script>
The actual thing is i am trying to hide my screen and put an ad in the modal until the page loads. Hope my question is clear. Thank you in Advance !!
Share Improve this question asked Jan 12, 2013 at 5:12 Vignesh GopalakrishnanVignesh Gopalakrishnan 1,9929 gold badges32 silver badges53 bronze badges 17-
You want to dynamically change the
width
andheight
of the modal when the window is zoomed in and out? – dunli Commented Jan 12, 2013 at 5:42 - Obviously you are right. – Vignesh Gopalakrishnan Commented Jan 12, 2013 at 5:47
-
Have you tried capturing the event when the window is resized? Like
$(window).resize(function(){ //Scale the width and height of modal });
– dunli Commented Jan 12, 2013 at 5:49 - No. I am just capturing the screen width and height. – Vignesh Gopalakrishnan Commented Jan 12, 2013 at 5:52
-
The
screen
width
is different from thewindow
width
. Screen width is taken from the monitors resolution. – dunli Commented Jan 12, 2013 at 5:56
2 Answers
Reset to default 3I have here an update in jsfiddle.
I have added this:
modal.css({
width: w + ($(window).width() - w),
height: h + ($(window).height() - h)
});
to your script. The good thing is that the modal
is occupying the whole screen, on the other hand when the window is resized the border (green) will be gone, only the contents of the modal are shown.
But it will be a good start for you.
UPDATE:
I've updated the jsfiddle here.
Hope it helps.
<script>
$(document).ready(function() {
(function () {
var width = screen.width,
height = screen.height;
setInterval(function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange', width, height);
}
}, 50);
}());
$.modal($("#basic-modal-content"));
$("#simplemodal-container").width(screen.width);
$("#simplemodal-container").height(screen.height);
});
$(window).on('resolutionchange', function(width, height){
$("#simplemodal-container").width(width);
$("#simplemodal-container").height(height);
//you also need to set position left:0 and top:0;
});
setTimeout(function() {
$.modal.close();
}, 15000);
</script>
but i suggestion to use this way, because you want fill browser window
#simplemodal-container {
left:0;
top:0;
width:100%;
height:100%;
position:fixed;
}
<script type="text/javascript">
setTimeout(function(){
$('#simplemodal-container').fadeOut();
},1500);
</script>
or in other way [your solution]:
<script>
$(function() {
var width = screen.width,
height = screen.height;
$.modal($("#basic-modal-content"));
$("#simplemodal-container").width(width).height(height).css({top:0,left:0});
$(window).resize(function(){
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange', width, height);
});
$(window).on('resolutionchange', function(width, height){
$("#simplemodal-container").width(width).height(height).css({top:0,left:0});
//if you have padding for modal, remove it
});
setTimeout(function() {
$.modal.close();
}, 15000);
});
</script>
本文标签:
版权声明:本文标题:javascript - Changing Height and width of modal dynamically when browser screen resolution changes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745416519a2657710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论