Pure Javascript slider on swipe for mobile









up vote
2
down vote

favorite












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?










share|improve this question



























    up vote
    2
    down vote

    favorite












    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?










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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?










      share|improve this question















      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?







      javascript html css html5 slider






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 18:33

























      asked Nov 10 at 22:44









      maxwell

      314




      314






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          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>







          share|improve this answer






















          • Is it showing one image while swiping ? , Is it getting back the previous images after swiping to the opposite direction?
            – maxwell
            Nov 11 at 17:33










          • @maxwell Check it here: jsfiddle.net/reddysridhar53/6ng5ocyp/9
            – sridhar reddy
            Nov 12 at 6:10











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244161%2fpure-javascript-slider-on-swipe-for-mobile%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          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>







          share|improve this answer






















          • Is it showing one image while swiping ? , Is it getting back the previous images after swiping to the opposite direction?
            – maxwell
            Nov 11 at 17:33










          • @maxwell Check it here: jsfiddle.net/reddysridhar53/6ng5ocyp/9
            – sridhar reddy
            Nov 12 at 6:10















          up vote
          0
          down vote













          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>







          share|improve this answer






















          • Is it showing one image while swiping ? , Is it getting back the previous images after swiping to the opposite direction?
            – maxwell
            Nov 11 at 17:33










          • @maxwell Check it here: jsfiddle.net/reddysridhar53/6ng5ocyp/9
            – sridhar reddy
            Nov 12 at 6:10













          up vote
          0
          down vote










          up vote
          0
          down vote









          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>







          share|improve this answer














          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>








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 6:04

























          answered Nov 11 at 17:06









          sridhar reddy

          56027




          56027











          • Is it showing one image while swiping ? , Is it getting back the previous images after swiping to the opposite direction?
            – maxwell
            Nov 11 at 17:33










          • @maxwell Check it here: jsfiddle.net/reddysridhar53/6ng5ocyp/9
            – sridhar reddy
            Nov 12 at 6:10

















          • Is it showing one image while swiping ? , Is it getting back the previous images after swiping to the opposite direction?
            – maxwell
            Nov 11 at 17:33










          • @maxwell Check it here: jsfiddle.net/reddysridhar53/6ng5ocyp/9
            – sridhar reddy
            Nov 12 at 6:10
















          Is it showing one image while swiping ? , Is it getting back the previous images after swiping to the opposite direction?
          – maxwell
          Nov 11 at 17:33




          Is it showing one image while swiping ? , Is it getting back the previous images after swiping to the opposite direction?
          – maxwell
          Nov 11 at 17:33












          @maxwell Check it here: jsfiddle.net/reddysridhar53/6ng5ocyp/9
          – sridhar reddy
          Nov 12 at 6:10





          @maxwell Check it here: jsfiddle.net/reddysridhar53/6ng5ocyp/9
          – sridhar reddy
          Nov 12 at 6:10


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244161%2fpure-javascript-slider-on-swipe-for-mobile%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo