[英]Change javascript according to window width
I think this is quite simple but after 2 days of trying I'm still clueless.我认为这很简单,但经过 2 天的尝试,我仍然一无所知。 Basically, I need to run one set of commands if the screen is over 767 pixels wide and another if the screen is under 767 pixels.基本上,如果屏幕宽度超过 767 像素,我需要运行一组命令;如果屏幕宽度低于 767 像素,我需要运行另一组命令。
When the screen is wider than 767 pixels, I want to:当屏幕大于 767 像素时,我想:
<script type="text/javascript">
var jsReady = false;//for flash/js communication
// FLASH EMBED PART
var flashvars = {};
var params = {};
params.quality = "high";
params.scale = "noscale";
params.salign = "tl";
params.wmode = "transparent";
params.bgcolor = "#111111";//change flash bg color here
params.devicefont = "false";
params.allowfullscreen = "true";
params.allowscriptaccess = "always";
var attributes = {};
attributes.id = "flashPreview";
swfobject.embedSWF("preview.swf", "flashPreview", "100%", "100%", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
<!-- and much more code... -->
</script>
When the screen is narrower than 768 pixels, I want to run:当屏幕小于 768 像素时,我想运行:
<script type="text/javascript">
jQuery(function($){
$.supersized({
//Background image
slides : [ { image : 'img/some_image.jpg' } ]
});
});
</script>
That's right... For desktops and tablets, I want to show a full-screen video background.没错...对于台式机和平板电脑,我想显示全屏视频背景。 For smaller screens (less than 767 pixels), I want to show a single still image background.对于较小的屏幕(小于 767 像素),我想显示单个静止图像背景。
if(screen.width > 767) {
code A...
} else {
code B...
}
You can get the currect size of windows using $(window).width()
and attach a handler on the resize event of the form.您可以使用$(window).width()
获取 windows 的当前大小,并在表单的调整大小事件上附加一个处理程序。 For a simple use, It is as simple as对于简单的使用,它就像
$(window).resize(funcion() {
$width = $(window).width();
if($width < 767) {
$.supersized({
//Background image
slides : [ { image : 'img/some_image.jpg' } ]
});
} else {
//if width is greater than 767
}
});
Since you're using JQuery you can use this:由于您使用的是 JQuery,因此您可以使用:
if ($(window).width > 767) { ... }
That returns the current size of the window, not the max.这将返回 window 的当前大小,而不是最大值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.