在动画中将div元素移动到屏幕中间

时间:2010-12-25 01:24:08

标签: javascript jquery

如何从左侧将div元素设置为屏幕中间

我得到了这个无效的obect或prperty 我似乎无法通过jQuery引用jQuery(文档)对象

form.each(function () {
    jQuery(this).animate({ marginLeft: jQuery(document).width / 2 }, 750); <-- this line here gives me errors
    jQuery('input[name^=Next]').click(function () {

    });
});

2 个答案:

答案 0 :(得分:2)

您需要使用.animate为div的marginLeft属性设置动画。看看这些例子。这是一个快速模拟:

$('#myDiv').animate({
    marginLeft: '+=' + $(document).width() / 2
}, 5000, function() {
    // Animation complete.
});

演示:http://jsfiddle.net/karim79/W6v2B/

答案 1 :(得分:1)

使用jQuery Animate

这是一个简单的例子:

<强> HTML:

<div id='walker'>Hello World</div>
<input type='button' id='move' value='move'>

<强> jQuery的:

jQuery(document).ready(function(){
   jQuery('#move').live('click', function(event) {        
       $("#walker").animate({marginLeft: $(window).width()/2,}, 1500 );
   });
});

Demo