0
所以我遇到了一个问题,通过.each运行一个函数,它似乎并没有通过'this'拾取有问题的元素。下面的代码:(编辑:我试图绝对中心的东西,无论屏幕宽度的多个元素)jQuery问题:通过每个元素运行函数并获取元素
$(document).ready(function(){
$('.main-feature h1').each(function() {
centerThis();
});
$('.meta-feature h2').each(function() {
centerThis();
});
});//doc-rdy
function centerThis(){
var trackHeight = $(this).height()/2;
var trackWidth = $(this).width()/2;
var pxInEmsHeight = Math.floor((trackHeight/146) * 100)/100;
var pxInEmsWidth = Math.floor((trackWidth/146) * 100)/100;
$(this).css({
"margin-left": -pxInEmsWidth+'em',
"margin-top": -pxInEmsHeight+'em'
});
$(window).resize(function(){
var trackHeight = $(this).height()/2;
var trackWidth = $(this).width()/2;
var pxInEmsHeight = Math.floor((trackHeight/146) * 100)/100;
var pxInEmsWidth = Math.floor((trackWidth/146) * 100)/100;
$(this).css({
"margin-left": -pxInEmsWidth+'em',
"margin-top": -pxInEmsHeight+'em'
});
});
}//centerThis
感谢Barmar,一杆进洞!生病了8米的投票:) – 2014-09-01 15:49:31