[英]jquery window.resize not working
伙计们,我写了一个非常简单的jquery脚本,将元素正确地放置在网页上,基本上使它可以响应,而无需媒体查询。 现在它可以正常工作,但是window.resize不会触发,因此document.ready可以在窗口调整大小时不会触发。 任何帮助将不胜感激。 将链接放在页面底部
// JavaScript Document
$(document).ready(PlaceContents);
$(window).ready(PlaceContents);
$(window).on('resize',PlaceContents);
function PlaceContents() {
var Wheight = $(document).height();
var Wwidth = $(document).width();
var contentPos = Wwidth/4;
var contentH = Wheight/5;
//adjust menubar height
$('#menubar').height(Wheight);
//center main content
$("#maincontent").css({top: contentH, left: contentPos, position:'absolute'});
};
希望这是您想要的。
window.addEventListener('resize', function() {
PlaceContents();
}, false);
您应该对此进行回调:
$(document).ready(function(){ PlaceContents(); }); function PlaceContents() { var Wheight = $(document).height(); var Wwidth = $(document).width(); var contentPos = Wwidth/4; var contentH = Wheight/5; //adjust menubar height $('#menubar').height(Wheight); //center main content $("#maincontent").css({top: contentH, left: contentPos, position:'absolute'}); };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="maincontent" method="POST"> <input type="submit" value="menubar" name="menubar" id="menubar"> </form>
您可以使用以下功能
$(window).bind("load resize", function () {
PlaceContents();
});
希望这会帮助你。
好吧,您可以像这样编写相同的代码:
页面加载后以及在发生resize事件时,都会运行resize函数。
祝好运 :)
$(document).ready(function(){
$(window).resize(function(){
var Wheight = $(window).height();
var Wwidth = $(window).width();
var contentPos = Wwidth/4;
var contentH = Wheight/5;
//adjust menubar height
$('#menubar').height(Wheight);
//center main content
$("#maincontent").css({top: contentH, left: contentPos, position:'absolute'});
}).resize();
});
试试这个代码:
$(document).ready(function(){
$(window).resize(function(){
var Wheight = $(window).height();
var Wwidth = $(window).width();
var contentPos = Wwidth/4;
var contentH = Wheight/5;
//adjust menubar height
$('#menubar').height(Wheight);
//center main content
$("#maincontent").css({top: contentH, left: contentPos, position:'absolute'});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.