我已经写了一些代码来折叠2列,如果左边的文本比右边的图像长:
var collapsed = null;
var banner = function() {
var txt = $('.banner .text').outerHeight();
var image = $('.banner .main-image').outerHeight();
if (txt > image) {
// Collapse columns
$('.banner').addClass('single-line');
// Set the breakpoint the column collapsed at
if (collapsed == null) {
collapsed = $(window).width();
}
}
// Restore the 2 columns when the browser hits the breakpoint
if ($(window).width() >= collapsed) {
$('.banner').removeClass('single-line')
}
}我的问题是让这个函数在正确的时间触发。这是可行的:
$(window).resize(banner);但这两种方法都不管用。
$(window).onload(banner); // When the page first loads
$(window).on('orientationchange', banner); // When device is rotated我在这里可能完全走错了路,所以请随时为我指出正确的方向。
提前感谢!
发布于 2016-06-01 21:46:18
根据JQuery的文档,jquery对象没有"onload“方法,而应该使用"ready":
$(window).ready(banner);关于这一行没有被触发,可能是因为你使用了错误的synthax。
.on(事件、选择器、处理程序)
所以你应该试试这个:
$(window).on('orientationchange','window', banner)干杯
发布于 2016-06-01 22:05:40
要在页面首次加载时运行,您可以使用:
$(document).ready(function() {
banner();
});关于orientationchange事件,请确保使用的是jquery.mobile.js (https://api.jquerymobile.com/orientationchange/)
https://stackoverflow.com/questions/37570021
复制相似问题