0
我想做一个函数,它返回“什么左值应该是中心当前元素”。这使我可以对中心(和后面)进行动画制作,或者只是将项目居中。如何使用jquery将相对定位的元素居中?
虽然我有相对定位部分的麻烦。我错过了什么?
谢谢!
jQuery.fn.centerHorizontalOffset = function() {
var position = $(this).css("position");
if (position == "relative"){
return (($(window).width() - $(this).width())/2) - $(this).parent().offset().left;
} else if (position == "absolute"){
return ($(window).width() - $(this).width())/2;
}
return ($(window).width() - $(this).width())/2;
}
jQuery.fn.centerHorizontalDistance = function() {
return $(this).centerHorizontalOffset()-$(this).position().left;
}
$('#myDiv').css('left', $('#myDiv').centerHorizontalDistance());
你可以创建一个http://jsfiddle.net你到目前为止包括你的HTML和CSS。对我来说,有点难以回答,看起来你可能会倒退。 – tw16
我结束了刚刚使用jQuery UI框架,谢谢! – chacham15