Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable dragging in specific element (Item) in “Owl carousel” - jquery

I use "OWL Carousel" jQuery plugin (http://www.owlgraphic.com/owlcarousel/) for in my small project.

I create small script that draggable via this plugin. now i want to disable dragging in specific element (Item)... but i don't know how to do that.

HTML:

<div class="wrap">
    <div id="carousel">

    <div class="part">
        <div class="item">Item-1.1</div>
        <div class="item">Item-1.2 NOT Draggble</div>
        <div class="item">Item-1.3</div>
    </div>
    <div class="part">
        <div class="item">Item-2.1</div>
        <div class="item">Item-2.2 NOT Draggble</div>
        <div class="item">Item-2.3</div>
    </div>
    <div class="part">
        <div class="item">Item-3.1</div>
        <div class="item">Item-3.2 NOT Draggble<div>
        <div class="item">Item-3.3</div>
    </div>

    </div>
</div>

CSS:

div.wrap {
    width: 990px;
    min-height: 360px;
    padding: 5px;
    border: 2px solid #666;
    margin: 10px auto;
}
div.item {
    float: left;
    border: 1px solid #ddd;
    width: 324px;
    margin: 1px;
    min-height: 360px; 

}

JS:

$(document).ready(function() {

    $("#carousel").owlCarousel({
        singleItem: true,
    startPosition : -1,
    autoPlayDirection : "rtl",

    });

});

I know I should use to mouseDrag (http://www.owlgraphic.com/owlcarousel/#customizing) in a callback function , but i don't now how ... :(

like image 852
Mohsen Rasouli Avatar asked Apr 07 '14 10:04

Mohsen Rasouli


1 Answers

I had the same problem and just figured it out:

Add a class to the items you want to disable (i.e. "disable-owl-swipe") and stop event propagation on touchstart and mousedown, so the parents (the owl carousel wrappers) don't receive the event and therefore don't swipe.

HTML:

<div class="wrap">
    <div id="carousel">

    <div class="part">
        <div class="item">Item-1.1</div>
        <div class="item disable-owl-swipe">Item-1.2 NOT Draggble</div>
        <div class="item">Item-1.3</div>
    </div>
    <div class="part">
        <div class="item">Item-2.1</div>
        <div class="item disable-owl-swipe">Item-2.2 NOT Draggble</div>
        <div class="item">Item-2.3</div>
    </div>
    <div class="part">
        <div class="item">Item-3.1</div>
        <div class="item disable-owl-swipe">Item-3.2 NOT Draggble<div>
        <div class="item">Item-3.3</div>
    </div>

    </div>
</div>

JS:

$(".disable-owl-swipe").on("touchstart mousedown", function(e) {
    // Prevent carousel swipe
    e.stopPropagation();
})

Hope that helps!

like image 84
heather Avatar answered Sep 21 '22 12:09

heather