【问题标题】:jquery animate position - absolute vs relativejquery动画位置 - 绝对与相对
【发布时间】:2012-04-11 07:21:48
【问题描述】:

我想将一组“可拖动”图像的初始位置存储为图像中的数据属性。然后,当按下“putBack”按钮时,图像将返回其原点。问题是图像返回到相对位置。它们被定位到 2XTop 和 2XLeft,而不是 Top & Left。

    // assign data attributes x,y data attributes       
    $('li img').each(function(index) {
        $(this).data("Left", $(this).parent().position().left)
               .data("Top", $(this).parent().position().top);       
        console.log($(this).data("Top"));
    });

    // button to put images back where they started 
    $("#putBack").bind('click', function() {
        $('li img').each(function(index) {

        console.log($(this).data("Top"));
             $(this).parent().animate(
                     { "left": $(this).data("Left"), 
                             "top": $(this).data("Top")
                     }, "slow");                    
        });

    });

html...

<ul>
<li id='apple'><img src="images/apple.png"/></li>
<li id='bread'><img src="images/bread.png"/></li>
<li id='banana'><img src="images/banana.png"/></li>
<li id='hot_dog'><img src="images/hot_dog.png"/></li>
<ul>

css...

ul{
   width:100px;
   padding:0px;
   list-style:none;
   font-size:20px;
}

【问题讨论】:

    标签: jquery layout position jquery-animate


    【解决方案1】:

    您似乎保留了父级 li 而不是 img 的位置。这是一个 js fiddle,它可以记住图像的位置并将它们相应地移回:jsfiddle.net/ps7WN

    【讨论】:

    • 关闭,但仍未返回原始位置。认为动画也有问题。
    • 可能不是动画的问题,而是元素的 CSS 定位问题。你能发布相关的CSS吗?
    • 就定位而言没有相关的css。
    • 如果你在移动图像,你的动画是错误的。见jsfiddle.net/ps7WN
    【解决方案2】:

    这成功了 -

    $(document).ready(function(){ 
        $('li').draggable({
                // revert: true,
                cursor: 'pointer',
                opacity: .4,
                containment: 'document'
        });
    
    
        // turn on and off dragging 
        $('.toggle').click(function() {
            if ($( "li" ).draggable( "option", "disabled" ) == true){
                $( "li" ).draggable( "option", "disabled", false );             
                $('.toggle').val('Prevent Draggable');
            }
            else{
                $( "li" ).draggable( "option", "disabled", true );              
                $('.toggle').val('Allow Draggable');
            }
        });
    
        // assign data attributes x,y data attributes       
        $('li img').each(function(index) {
            $(this).data("Left", $(this).parent().offset().left)
                   .data("Top", $(this).parent().offset().top);     
            console.log($(this).data("Top"));
        });
    
        // button to put images back where they started 
        $("#putBack").bind('click', function() {
            $('li img').each(function(index) {                      
                 $(this).parent().animate(                   
                     { "left": 0, 
                             "top": 0
                     }, "slow");                    
            });
        });
    }); 
    

    【讨论】:

      猜你喜欢
      • 2011-01-13
      • 1970-01-01
      • 2012-03-21
      • 2015-09-21
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      相关资源
      最近更新 更多