Code Monkey home page Code Monkey logo

itemslide's Introduction

ItemSlide

A simple & beautiful vanilla JavaScript touch carousel

Features

  • Touch swiping
  • Mousewheel scrolling
  • The ability to "swipe out" slides
  • Centered carousel or left sided (default is centered)

Documentation

Getting Started

Markup

    <div id="scrolling">
        <ul>
            <li>Slide #1</li>
            <li>Slide #2</li>
        </ul>
    </div>

CSS

For this example CSS, we assume the carousel is contained within an element that has the id "scrolling"

#scrolling {
    overflow: hidden;
}

#scrolling ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    position: absolute;
    transform-style: preserve;
}

#scrolling ul li {
    float: left;
}

Include Script

<script src="itemslide.js"></script>

Initialize

var itemslide;

window.addEventListener("load", () => {
    var element = document.querySelector("#scrolling ul");
    itemslide = new Itemslide(element, {});
});

Options

Options are passed as key-values into the object that the Itemslide constructor gets into the second parameter, for example:

new Itemslide(element, { duration: 100 });

Will initialize Itemslide with a custom duration of 100ms.

Here are the available options:

  • duration - duration of slide animation {default: 350ms}
  • swipe_sensitivity - swiping sensitivity {default: 150}
  • disable_slide - disable swiping and panning {default: false}
  • disable_clicktoslide - disable click to slide {default: false}
  • disable_autowidth - disable the automatic calculation of width (so that you could do it manually) {default: false}
  • disable_scroll - disable the sliding triggered by mousewheel scrolling {default: false}
  • start - index of slide that appears when initializing {default: 0}
  • pan_threshold - can be also considered as panning sensitivity {default: 0.3}(precentage of slide width)
  • one_item - set this to true if the navigation is full screen or one slide every time. {default: false}
  • parent_width - set this to true if you want the width of the slides to be the width of the parent of ul. {default: false}
  • swipe_out - set this to true to enable the swipe out feature. {default: false} (
  • left_sided - left sided carousel (instead of default force-centered) {default: false}

Methods

  • getActiveIndex() - get current active slide index
  • getCurrentPos() - get current position of carousel (pixels)
  • nextSlide() - goes to next slide
  • previousSlide() - goes to previous slide
  • gotoSlide(i) - goes to a specific slide by index
  • reload() - recalculates width and center object (recommended to call when resize occures)
  • addSlide(data) - adds in the end of the carousel a new item.
  • removeSlide(index) - removes a specific slide by index.

NOTE: addSlide automatically adds li tags.

Events

ItemSlide triggers the following events on the element it is initialized on:

  • carouselChangePos - triggered when the position of the carousel is changed
  • carouselPan - triggered when user pans
  • carouselChangeActiveIndex - triggered when the current active item has changed
  • carouselSwipeOut - triggered when user swipes out a slide (if swipe_out is enabled)
    • event.slideIndex - get index of swiped out slide
  • carouselClickSlide - triggered when clicking/tapping a slide
    • event.slideIndex - get index of the clicked slide

Classes

The current active slide gets the 'itemslide-active' class.

Extras

  • attribute 'no-drag'- If you want to disable dragging only on a certain element in the carousel just add this attribute to the element. (example: <li no-drag="true">)

itemslide's People

Contributors

alexvvx avatar artemsky avatar dependabot[bot] avatar dixalex avatar f0rmat1k avatar frux avatar hy9be avatar itemslide avatar nir9 avatar strydom avatar tdzienniak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

itemslide's Issues

if "left_sided: true" the "click to slide" doesn't work

I've tested with "left_sided: true" and the "click to slide" doesn't work.

My code:

<script src="jquery-3.0.0.min.js"></script>
<script src="itemslide.min.js"></script>
<script src="jquery.mousewheel.min.js"></script>
<script>
var carousel;

$(document).ready(function () {
    carousel = $(".images");

    carousel.itemslide({
        left_sided: true
    }); //initialize itemslide

    $(window).resize(function () {
        carousel.reload();
    }); //Recalculate width and center positions and sizes when window is resized
});
</script>

Width default "left_sided: false" everything is fine.

Compatibility detection

Hello there!
Itemslide works great with all devices i've tested except Nokia Lumia 800's IE. Is there any JS workaround to detect wether it is compatible with current device?

Pinch to zoom

Hi

Is there any way to enable pinch to zoom on mobile devices? In my application, my users sometimes want to zoom into an image in the itemslide carousel. I notice that this is not possible. I've tested on Android and iPhone.

Take for example https://itemslide.github.io/ If you try to zoom in, it will not work. Any idea's?

Scrolling trigged by vertical double finger swipe on Macbook is override

My HTML is

<div id="frame">
            <ul>
                <li>
                    <div class="content"></div>
                </li>
                <li>
                    <div class="content"></div>
                </li>
            </ul>
        </div>

And the Javascript code is:

var carousel;
        $(document).ready(function () {

            carousel = $("#frame ul");

            carousel.itemslide({
                disable_scroll: false,
            }); //initialize itemslide

            $(window).resize(function () {
                carousel.reload();

            }); //Recalculate width and center positions and sizes when window is resized
        });

But no matter what direction I do the double finger swipe, the

  • will slide. The default scrolling action for div#content is override.

  • addSlide not functioning correctly

    'addSlide(data)' does not seem to be working as expected.

    1. When a line "carousel.addSlide("TEST");" is added to sliding.js, the
    2. element with "TEST" is appended as a child of the last
    3. element.
    4. An error occurs, and it says that b.reload (carousel.reload in itemslide.js) is not a function.
      Would you be able to take a look ?
      Thank you.

    addslide

    Copyright and permission notice are not included in

    According to the MIT license in your License file, "The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software", but the uglified versions you list for download do not include the notices. Does this mean that I am in violation of the MIT license if I distribute those files as part of my application?

    Feature: Reposition on window resize (without .reload())

    Hi.
    I am pretty sure that you need to add "reposition" event.

    Explaining why:

    At my webapp, by selecting slider item its load content to another window, which has an ifarme inside (youtube video).
    1st i tried to use Slider.reload() on window.resize() event
    But i met a problem after i opened my video at full screen. It called window.resize() event. And slider did reload, and my ifame with my video just gone.

    So i found solution by myself, and hope you add this event, because i like your lib!

    let width = 0; this.Slider.find("li").each(function(){ width+= $(this).outerWidth(); }); this.Slider.css("transform", translate3d(($(window).width() - width - this.Slider.find(".itemslide-active").outerWidth())/2 + "px, 0px, 0px)");`

    ItemSlide on a container without any element

    Hey there,

    if you try to apply the itemslide on a container without any children you get the error:

    Uncaught TypeError: Cannot read property 'offsetWidth' of undefined

    Patch (check length):

    // Stuff to add for compatibility with Zepto
    $.fn.outerWidth = function () {
    if (this.length) {
    var el = $(this)[0];
    var width = el.offsetWidth;
    var style = getComputedStyle(el);
    width += parseInt(style.marginLeft) + parseInt(style.marginRight);
    return width;
    } else {
    return 0;
    }
    };

    P.s: Sorry for not using git correctly :(

    Howto Vanilla + Browserify

    I'm trying to make itemslide work within a vue.js app.
    I used npm in order to install itemslider, then with browserify I tried

    var itemslide = require("itemslide")

    but it didn't work because I don't use/need jQuery.

    So i did copy the itemslide.vanilla.min.js inside my app and tried to

    var itemslide = require("./itemslide.vanilla.min.js")

    but it failed because itemslide is not a function.

    I tried

    global.itemslide = require("./itemslide.vanilla.min.js")

    and

    module.exports = require("./itemslide.vanilla.min.js")

    but none works.

    Did I miss somethnig ? Is there any clue on using vanilla itemslide and browserify ?

    Not working in latest Safari on Mac

    The demo on your site works like a charm. The files downloaded from GitHub are not working. I will let you know if I can figure anything out.

    Prev & next Buttons

    Is there anyone who uses prev and next buttons in itemslide.js?

    Need help.

    Not listening to Vertical Scroll touch

    Each of my li tag item has variable height(more than window height). So using overflow: scroll. And it works fine in browser. But when i emulate , the vertical scroll touch is not listened by the plugin.

    Spoils jquery's outerWidth

    The plugin overwrites jQuery's outerWidth function and does so without replicating all its functionality. Which is bad. Real bad. The code has to check the existence of the function:

    // Stuff to add for compatibility with Zepto
    if (!$.fn.outerWidth) {
        $.fn.outerWidth = function () {
            if($(this)[0] instanceof Element){
                var el = $(this)[0];
                var width = el.offsetWidth;
                var style = getComputedStyle(el);
    
                width += parseInt(style.marginLeft) + parseInt(style.marginRight);
                return width;
            }
        };
    }
    

    Multiple instances on same page - all scroll together

    What is the best way to implement multiple instances of itemslide on the same page? Currently all instances scroll together, they need to scroll individually.

    My current initialization:

    var carousel;
    carousel = jQuery(".post-image-container ul");
    carousel.itemslide();

    ability to overwrite id='active'

    In a lot of circumstances id=active is not ideal. The most simplest case as having multiple sliders on the page: 2 DOM-elements end up getting id=active which is not compliant.

    What about a way to overwrite this behavior, or (and this may work well enough) generate some id's and keep a list of those generated. I.e.: itemslide-1, itemslide-2. etc.

    'Active' setting not working as expected

    Thanks for a superb carousel. I revisited the docs and determined that to re-set the default active slide I have to add "start: '2', ( or whatever the slide number is, counting from 0). This is not working as expected- dunno if I did something wrong. The advantage of being able to set an altered view is that the visitor likely will end up swiping in both directions and hopefully, linger longer. Thanks for any pointers about how this can be done. My settings:

    <script> $(window).load(function() { $("#scrolling ul").itemslide({ disable_clicktoslide: 'false', disable_slide: 'false', one_item: 'true', parent_width: false, duration: '350', swipe_sensitivity: '290', disable_autowidth: true, swipe_out: false, start:'2', }); }); </script>

    Method to get when photo changed

    $(document).ready(function(){
    
        localStorage.removeItem("li_atual");
    });
    
    function posicaoAtual(){
    
        localStorage.setItem("li_atual",$("ul").getActiveIndex());
        console.log("mudou de foto");
    }
    
    $("ul").on('changePos', function(event) {
    
        if(Number(localStorage.getItem("li_atual")) != Number($("ul").getActiveIndex())){
        posicaoAtual();
        }
    });
    

    Increasing the performance in Cordova Native App

    I am using this in an hybrid mobile Application. When converted into corresponding native app, It works a bit ok in top end mobile such as nexus and all. But When it comes to a bit normal mobile, the transition is very slow. The feel of touch looks clumsy. Can you please suggest some ways where transition does not slow. I am really in need of this.(duration is already set 25ms)

    Events Not Triggered

    pan event , changeActiveIndex event is not triggered.
    And changePos event is triggered infinitely.
    Can u fix soon.

    Loop Slides

    Would it be possible to loop slides as a continuous carousel?

    Set start and end Point

    I was wondering if someone know how to make the first item stick to the left instead of the middle when selected (same for the right). I found a way to do it but it cut the velocity to 0 when the animation reach the limit.

    Thank you.

    Changing .itemslide-active while moving

    Currently, the CSS class is set when the movement is done.
    I need a CSS class which is updated while dragging so the user can adjust the exact position before releasing his finger. This might be another CSS class like .itemslide-currently-active if you wish to.

    Destroy the slider ? Method .destroy()

    Hi,

    I would like to totally destroy() the slider (I only use the slider on a specific screen-size), to avoid issue on screen resize. I would like to destroy the slider, and reinit it on a other breakpoint.

    But I don't found any .destroy() method?

    Any idea ? Plan to implement one ?

    Thanks.

    Fix the doc, the base css is wrong

    The docs says that the needed base css is this one :

    ul {
        margin: 0;
        padding: 0;
        list-style-type: none;
        position: absolute;
        -webkit-transform-style: preserve-3d;
        -ms-transform-style: preserve-3d;
        transform-style: preserve-3d;
    }
    
    li {
        float: left;
    }

    Meanwhile this line https://github.com/itemslide/itemslide.github.io/blob/master/src/itemslide.js#L81, says otherwise.
    You actually need to put at least left: 0; in your css otherwise initialLeft is NaN (because the default css value is auto) and therefore every calculation fails afterwards.

    Took me a while to figure this one out, if you could update the doc to prevent somebody else running in the same issue it would be great :)

    jQuery.noConflict() - Cannot read property 'extend' of undefined

    I have a mains scripts managing my javascript, using the jQuery.noConflict() to make sure there is no conflict with any other librairies loaded (and I don't have the total hand on which libraries are loaded).

    When I initialize itemslider it throw this error

    Uncaught TypeError: Cannot read property 'extend' of undefined

    There is an example of my code.

    var noConflictjQuery = jQuery.noConflict();
    (function ($) {
        $(function () {
           'use strict';
    
           var carousel = $("#categoryFilterList");
           carousel.itemslide();
        });
    })(noConflictjQuery);
    

    If I remove the var jQuery.noConflict(); everything work's, but I can't really afford that. Any idea ?

    Why ul and li are obligatory?

    Sometimes this limitations complicate development. Maybe just root and .children() matching?
    I think the semantics are not necessarily the slider must be a list.

    Why $.fn.itemslide placed into $.ready?

    e.g. my builder builded so:

    $(function(){
        $('.slider').itemslide();
    });
    ...
    $(function(){
       $.fn.itemslide = function(){
        ///...
       }
    });

    And i get error itemslide is not a function. So itemslide requires to be included before all, which is not always feasible. The above example actually happened to me. I think plugin users should make a decision about how to initialize itself. You don't think so?

    Multiple Itemslide's on a page

    Hi, I'm working on a project in which I have several itemslides on a page. Right now, I've been working with this workaround, but it's giving me some issues:

    var carousel_1;
    var carousel_2;
    $(document).ready(function() {
        $('.outfit a').on('click',function(e) {
            e.preventDefault();
            $(this).toggleClass('liked');
        });
    
    
        carousel_1 = $("#outfit_1");
        carousel_1.itemslide();
        carousel_2 = $("#outfit_2");
        carousel_2.itemslide();
    });
    $(window).resize(function () {
        carousel_1.reload();
        carousel_2.reload();
    });
    

    The problem is that the second carousel is causing the page to be very wide, which just scrolls the whole page on iPhone4.

    See a demo here:
    http://beta.jackie-lee.com/mcgregor/during.html

    Do you have any suggestions for multiple carousels on one page?

    Difference between click and swipe

    Hi

    I'd like to include an event on tapping / clicking on a slide.
    In my setup, I have a list of images that I'd love to enlarge upon click.

    As of now, swiping also triggers the onclick event, so I have to include another button, which complicates things.
    You can see an example here: http://beta.jackie-lee.com/mcgregor/during.html

    Is there any way to programmatically find the difference between swipe and click?
    Thanks in advance for your remarks. Everything works very nicely I might add.

    IPhone 4 doesn't work

    IPhone 4 doesn't work (as advertized on the home page).
    Using Safary on stock iOS 5.1
    No errors in debug console, but the slide container is empty.
    Maybe it's better to say smth like "works on Mobile Safary 7+", than ambiguous "iPhone 4".

    Bower

    Hey guys, I want to install in plugin like a bower component.

    Please, add this feature!

    Blurry text

    Because of the tanslate3d, text is very blurry. Isn't it possible to translateX and translateY to avoid this?

    Anchor (Click event) not firing

    HTML:

    Javascript:

    $(document).ready(function () {

    $("#blogslide ul").itemslide({
        one_item: true //Set this for proper full screen navigation
    }); //initialize itemslide
    
    $(window).resize(function () {
        $("#blogslide ul").reload();
    
    }); //Recalculate width and center positions and sizes when window is resized
    
    $('#blogslide a').click(function(e){
            e.preventDefault();
            console.log("this is the click");
        });
    

    });

    Test Link 1 is working.
    Test Link 2 is not working and MouseOver, Click event is not fired

    Jquery version: 1.11.2

    Wrong calculation with different item size

    When the first item with small width, slide will display strange

    HTML:

    <div class="itemSlider">
        <ul>
            <li style="width:299px;height:500px;background:#000;"></li>
            <li style="width:599px;height:500px;background:#F00;"></li>
            <li style="width:499px;height:500px;background:#0F0;"></li>
            <li style="width:699px;height:500px;background:#00F;"></li>
        </ul>
    </div>
    

    JS:
    itemSlider.itemslide({ disable_clicktoslide:true, left_sided:true, disable_scroll:true });
    result :
    1455616185397

    addSlide with drag events

    If im adding slides (because i have to load them...) they dont have the events for dragging...

    The relead function didnt update the events at all.

    Sorry i'm not able to make a nice fix for this. I had to patch it very hard in your code :/

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google โค๏ธ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.