开发者

Block draggable element from appearing after another

I have two draggable elements on the same x axis. I want to reproduce a slider effect, I mean slider 1 can't go af开发者_如何学Pythonter slider 2.

I don't know how to do that. Can anyone help me?

I use jQuery and the draggable plugin from jQuery UI.

EDIT :

Code : HTML :

<div id="chrono-bar">
<div id="slider-left" class="slider-left draggable obstacle"></div>
<div id="slider-right" class="slider-right draggable"></div>
<ul class="chrono-bar-ul block">
    <li class="chrono-bar-item">Janvier</li>
    <li class="chrono-bar-item">Février</li>
    <li class="chrono-bar-item">Mars</li>
    <li class="chrono-bar-item">Avril</li>
    <li class="chrono-bar-item">Mai</li>
    <li class="chrono-bar-item">Juin</li>
    <li class="chrono-bar-item">Juillet</li>
    <li class="chrono-bar-item">Aout</li>
    <li class="chrono-bar-item">Septembre</li>
    <li class="chrono-bar-item">Octobre</li>
    <li class="chrono-bar-item">Novembre</li>
    <li class="chrono-bar-item last">Décembre</li>
</ul>
<div class="chrono-bar-footer">
    <div class="line"></div>
</div>

Javascript :

<script>
$("#slider-left").draggable({ 
    axis: 'x', 
    containment: 'parent',
    'snap': '#slider-right', 
    collide: 'block',
    drag: function() {
        console.log($('#slider-left').offset().left);
    }
});
$("#slider-right").draggable({ axis: 'x', containment: 'parent','snap': '#slider-left' });


Your containment for slider-left should be slider-right if you don't want slider-left to go after slider-right. Thanks.


My solution was to change on each drag the containment area. I share my code with you :

var left = $('#chrono-bar').offset().left;
$("#slider-left").draggable({ 
    axis: 'x', 
    containment: [left,100,left+944-46,100],
    'snap': '#slider-right', 
    collide: 'block',
    drag: function() {
        $("#slider-right" ).draggable( "option", "containment", [$(this).position().left+left+46-5, 100, left+944-46, 1000] );
        autoWidth = $("#slider-right" ).position().left - $(this).position().left-46;
        $('#slider-middle-auto').css({width:autoWidth+'px', left: $(this).position().left + 46+'px'});
        $("#slider-left").css({'background-position':'0px -68px'});
    },
    stop:function(e) {
        autoWidth = $("#slider-right" ).position().left - $(this).position().left-46;
        $('#slider-middle-auto').css({width:autoWidth+'px', left: $(this).position().left + 46+'px'});
        $("#slider-left").css({'background-position':'0px 0px'});
        refreshListEvent(e);
    },
    grid : [77,77]
});
$("#slider-right").draggable({ 
    axis: 'x', 
    containment: 'parent',
    'snap': '#slider-left', 
    drag : function() {
        $("#slider-left" ).draggable( "option", "containment", [left-5, 100, $(this).position().left+left, 1000] );
        autoWidth = $(this).position().left - $("#slider-left" ).position().left - 46;
        $('#slider-middle-auto').css({width:autoWidth+'px', left: $("#slider-left" ).position().left + 46+'px'});
        $("#slider-right").css({'background-position':'0px -68px'});
    },
    stop:function(e) {
        autoWidth = $(this).position().left - $("#slider-left" ).position().left - 46;
        $('#slider-middle-auto').css({width:autoWidth+'px', left: $("#slider-left" ).position().left + 46+'px'});
        $("#slider-right").css({'background-position':'0px 0px'});
        refreshListEvent(e);
    },
    grid : [77,77]
});

$("#slider-right" ).draggable( "option", "containment", [$("#slider-left" ).position().left+left+46, 100, left+944-46, 1000] );
$("#slider-left" ).draggable( "option", "containment", [left-5, 100, $("#slider-right" ).position().left+left, 1000] );

autoWidth = $("#slider-right" ).position().left - $("#slider-left" ).position().left - 46;
$('#slider-middle-auto').css({width:autoWidth+'px', left: $("#slider-left" ).position().left + 46+'px'});

There is some bugs, but i think it's a good start !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜