我一直试图在我的页面上创建一个jquery效果中心,而不是向右移动约150px。是否存在另一种保持这种效果的方法,但确保div是中心。
$(window).load(function() {
var currWidth = $(window).width();
console.log(currWidth);
var startPos = -100;
var endPos = (currWidth / 2) + (startPos / 2);
console.log(endPos);
$('#welcome').animate({left: endPos}, 1000);
});
答案 0 :(得分:1)
$(window).load(function() {
var currWidth = $(window).width();
var element = 270; //half the size of the element
var put = (currWidth / 2) - element; //work out the margin left to put the element
$('#welcome').animate({left: put}, 1000);
});