jQuery函数在第一页加载时不触发,但在刷新时会触发fine
jQuery Function not triggering on first page load, but triggering fine on refresh
我使用下面的函数在网格行上设置等高列。在初始页面加载时,函数将div.gallery-item
上的高度设置为"0"。刷新时,函数会正确启动,并按照函数中的规定指定高度。
这是脚本:
/* Responsive equal height columns for gallery (http://css-tricks.com/examples/EqualHeightsInRows/) */
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array();
function setConformingHeight(el, newHeight) {
// set the height to something new, but remember the original height in case things change
el.data("originalHeight", (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight")));
el.height(newHeight);
}
function getOriginalHeight(el) {
// if the height has changed, send the originalHeight
return (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight"));
}
function columnConform() {
// find the tallest DIV in the row, and set the heights of all of the DIVs to match it.
$('div.gallery-item').each(function() {
// "caching"
var $el = $(this);
var topPosition = $el.position().top;
if (currentRowStart != topPosition) {
// we just came to a new row. Set all the heights on the completed row
for(currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);
// set the variables for the new row
rowDivs.length = 0; // empty the array
currentRowStart = topPosition;
currentTallest = getOriginalHeight($el);
rowDivs.push($el);
} else {
// another div on the current row. Add it to the list and check if it's taller
rowDivs.push($el);
currentTallest = (currentTallest < getOriginalHeight($el)) ? (getOriginalHeight($el)) : (currentTallest);
}
// do the last row
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);
});
}
$(window).resize(function() {
columnConform();
});
$(function() {
columnConform();
});
我知道这与我加载函数的方式有关。在我编写这个脚本的文章中,建议使用window.onload
,如果图像是设置列上不同高度的原因,但我已经尝试将其添加到脚本的最后部分,如下所示,该函数根本不会触发。
$(window).onload = (function() {
columnConform();
});
有什么建议吗?
您应该使用:
window.onload = columnConform;
而不是:
$(function () {
columnConform();
});
通过这种方式,图像的大小以及父容器都是相关的。
您是否尝试将代码隐藏在$( document ).ready( handler )
段中而不是加载?
有关.ready()
的更多信息,请访问jQuery文档页面。
相关文章:
- 强制模板刷新ember.js
- 如何通过ajax刷新JSF填充的javascript变量
- 如何解决Yii中的页面刷新问题
- Jquery提交表单而不刷新
- 刷新页面后会出现警报
- 刷新后保留对网页的更改
- 如何在不刷新页面的情况下更新显示框
- 重定向时角度刷新浏览器
- 如何防止jQueryonclick事件中的Ruby方法在页面刷新时执行
- Javascript,输出结果后页面不断刷新
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- 刷新父窗口后无法关闭窗口
- 刷新页面时hasClass不起作用
- X秒后刷新select元素
- chart.series[id].remove()无法刷新高位图表/高位股票中其他系列的图例属性
- 触发媒体查询断点时刷新页面
- 如何刷新列表框内容onclick或blur事件
- 如何在火狐浏览器中禁用F5和刷新
- 刷新后,setTimeout将工作或不工作
- jQuery函数在第一页加载时不触发,但在刷新时会触发fine