Так что у меня проблема с функцией через .each. Это jQuery, похоже, не подбирает этот элемент через 'this'. Код ниже: (Edit: Я пытаюсь центрирование что-то, независимо от ширины экрана абсолютно для нескольких элементов)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 м :) –