简体   繁体   中英

Pure Javascript slider on swipe for mobile

I have about 6 images that I would like to create a slider for on mobile, For small screens only.

I searched about it and most of the answers were some libraries and Jquery, But I want it to be in pure Javascript without any libraries.

I created a fiddle of what I tried, This is a very simple snippet that moves the first image to the left on swipe and then moves it back to its previous position : http://jsfiddle.net/dkmp5h1L/3/

Here is the code: Js code:

function swipe(event){
    var midpoint = Math.floor(screen.width/2),
        touch = event.targetTouches[0],
        px = touch.pageX,
        firstItem = document.getElementById('firstItem'); 
    if(px > midpoint){
        firstItem.style.marginLeft = '-100%';
        firstItem.style.transition = '1s ';
    }else{
        firstItem.style.marginLeft = '0';
        firstItem.style.transition = '1s ';          
    }
}

CSS:

.images-gallary{
  background-color: #000;
  overflow: hidden;
  height: 73px;
  white-space: nowrap;
}

.image-wrapper{
  width: 100%;
  display: inline-block;
  text-align: center;
}

#secondItem{
  background: #fff;
}

HTML:

<div class="images-gallary" ontouchmove="swipe(event)">
  <div class="image-wrapper" id="firstItem">
    <img class="image" src="http://placehold.it/73/200">
  </div>
  <div class="image-wrapper" id="secondItem">
    <img class="image" src="http://placehold.it/73/300">
  </div>
  <div class="image-wrapper">
    <img class="image" src="http://placehold.it/73/400">
  </div>
</div> <!-- .images-gallary -->

So ideally what I want is 3 images next to each other and another 3 hidden images, the user can swipe to the left to see the hidden images at the left, Then to the right to show the ones those becomes hidden at the right.

Just like google website on mobile on searching, Just make the browser width smaller or visit it from mobile, Then search for 'image' for example, You will see 3 images beneath google search box that you can slide to show more images.

How to achieve that?

Have an access to the current active slide and add CSS accordingly. I have tries a sample code and it seems to be working. Check it.

        <!DOCTYPE html>
            <html lang="en">
                <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
    .images-gallary{
        overflow: hidden;
        height: 73px;
        white-space: nowrap;
    }

    .image-wrapper{
        width: 100%;
        display: inline-block;
        text-align: center;
    }
    #firstItem {
        background: #a8a8a8;
    }
    #secondItem{
        background: #c8c8c8;
    }
    #thirdItem{
        background: #d8d8d8;
    }
</style>
</head>
<body>
<div class="images-gallary">
  <div class="image-wrapper" id="firstItem">
    <img class="image" src="http://placehold.it/73/200" />
  </div>
  <div class="image-wrapper" id="secondItem">
    <img class="image" src="http://placehold.it/73/300" />
  </div>
  <div class="image-wrapper" id="thirdItem">
    <img class="image" src="http://placehold.it/73/400" />
  </div>
</div>
<button onClick="swipe(event, 'left')">left</button>
<button onClick="swipe(event, 'right')">right</button>
<script>
        var ImageIndex = 0;
        function swipe(event, direction){
            var midpoint = Math.floor(screen.width/2);
            var px = event.pageX;
            var items = document.getElementsByClassName('image-wrapper');
            var itemActive = items[ImageIndex]; 
            if (direction === 'left') {
                itemActive.style.marginLeft = '-100%';
                itemActive.style.transition = '1s ';
                ImageIndex = ImageIndex < items.length - 1 ? ImageIndex + 1 : ImageIndex;
            }else{
                itemActive.style.marginLeft = '0';
                itemActive.style.transition = '1s ';
                ImageIndex = ImageIndex >= 1 ? ImageIndex - 1 : 0;
            }
        }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM