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?
javascript html css html5 slider
add a comment |
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?
javascript html css html5 slider
add a comment |
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?
javascript html css html5 slider
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
javascript html css html5 slider
edited Nov 11 at 18:33
asked Nov 10 at 22:44
maxwell
314
314
add a comment |
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>
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>
edited Nov 12 at 6:04
answered Nov 11 at 17:06
sridhar reddy
1347
1347
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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