Creating simple pure Javascript slider on swipe for mobile devices
I want to create something similar to Google slider for images on searching on small screen sizes, Just make the browser width smaller or visit it from mobile, Then type 'image' in Google search box for example, You will see 3 images beneath Google search box that you can slide to show more images.
So there will be about 6 images next to each other, But 2 are fully shown and about half of the 3rd image, While the 3 others will be hidden.
As the user swipe to the left the 3rd image and the other 3 would become visible, The user should still can swipe to the right to show the first images if he already swiped to the left.
I tried to analyze the one on Google, But there are elements those I see for the first time, And I don't know how to find the related JS functions, If this is done with pure JS.
That's what I tried so far:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < items.length) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = 0;
}
}
}
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 -->
This code just slide the first 2 images, And I get an error:
Uncaught TypeError: Cannot read property 'style' of undefined
So how to customize that code to work as mentioned at the top?
I don't want to use Jquery or any libraries, Just pure JS code.
javascript html css html5 css3
add a comment |
I want to create something similar to Google slider for images on searching on small screen sizes, Just make the browser width smaller or visit it from mobile, Then type 'image' in Google search box for example, You will see 3 images beneath Google search box that you can slide to show more images.
So there will be about 6 images next to each other, But 2 are fully shown and about half of the 3rd image, While the 3 others will be hidden.
As the user swipe to the left the 3rd image and the other 3 would become visible, The user should still can swipe to the right to show the first images if he already swiped to the left.
I tried to analyze the one on Google, But there are elements those I see for the first time, And I don't know how to find the related JS functions, If this is done with pure JS.
That's what I tried so far:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < items.length) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = 0;
}
}
}
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 -->
This code just slide the first 2 images, And I get an error:
Uncaught TypeError: Cannot read property 'style' of undefined
So how to customize that code to work as mentioned at the top?
I don't want to use Jquery or any libraries, Just pure JS code.
javascript html css html5 css3
add a comment |
I want to create something similar to Google slider for images on searching on small screen sizes, Just make the browser width smaller or visit it from mobile, Then type 'image' in Google search box for example, You will see 3 images beneath Google search box that you can slide to show more images.
So there will be about 6 images next to each other, But 2 are fully shown and about half of the 3rd image, While the 3 others will be hidden.
As the user swipe to the left the 3rd image and the other 3 would become visible, The user should still can swipe to the right to show the first images if he already swiped to the left.
I tried to analyze the one on Google, But there are elements those I see for the first time, And I don't know how to find the related JS functions, If this is done with pure JS.
That's what I tried so far:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < items.length) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = 0;
}
}
}
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 -->
This code just slide the first 2 images, And I get an error:
Uncaught TypeError: Cannot read property 'style' of undefined
So how to customize that code to work as mentioned at the top?
I don't want to use Jquery or any libraries, Just pure JS code.
javascript html css html5 css3
I want to create something similar to Google slider for images on searching on small screen sizes, Just make the browser width smaller or visit it from mobile, Then type 'image' in Google search box for example, You will see 3 images beneath Google search box that you can slide to show more images.
So there will be about 6 images next to each other, But 2 are fully shown and about half of the 3rd image, While the 3 others will be hidden.
As the user swipe to the left the 3rd image and the other 3 would become visible, The user should still can swipe to the right to show the first images if he already swiped to the left.
I tried to analyze the one on Google, But there are elements those I see for the first time, And I don't know how to find the related JS functions, If this is done with pure JS.
That's what I tried so far:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < items.length) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = 0;
}
}
}
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 -->
This code just slide the first 2 images, And I get an error:
Uncaught TypeError: Cannot read property 'style' of undefined
So how to customize that code to work as mentioned at the top?
I don't want to use Jquery or any libraries, Just pure JS code.
javascript html css html5 css3
javascript html css html5 css3
edited Nov 11 at 22:52
asked Nov 11 at 19:04
maxwell
314
314
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
So the problem in your case is in this condition if (ImageIndex < items.length) {
. You have total amount of 3 elements, therefore when ImageIndex
is 2, it will increment it to 3
and as you have 3 images in the html items[ImageIndex]
will return undefined.
So change the condition to if (ImageIndex < items.length - 1) {
.
You are right that fixed the error, But how to implement the 2 and half images idea?
– maxwell
Nov 11 at 19:22
What should be a desired behavior, when in the initial state the user swipes left. It should move to the second image or the third one?
– Oleksandr Fedotov
Nov 11 at 19:33
There are 6 images right now, I want 1 of the 2 following cases: 1- 2 images are fully displayed and part of the 3rd image and the last 3 images are hidden, When the user swipes left the hidden parts of the 3rd image would be shown and as swiping the other 3 images would be shown too, And for sure if the user swiped to the left he could swipe to the right again
– maxwell
Nov 11 at 20:42
Case 2: 3 images/1 image are/is shown and the user can swipe to show the 3 others as he is swiping.
– maxwell
Nov 11 at 20:43
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
|
show 1 more comment
Replace your first condition ImageIndex < items.length
with ImageIndex < (items.length - 1)
because total images length is 3 here and trying to access style of items[3]
(undefined
)
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else {
ImageIndex = 0;
}
and one more thing in 2nd condition you should have replace ImageIndex = 0;
with ImageIndex = items.length - 1;
in else condition because always initialising same value ImageIndex = 0;
in case of px > midpoint
and ImageIndex < 1
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else {
ImageIndex = items.length-1;
}
your final code seems like below:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = items.length - 1;
}
}
}
Thanks I updated it, Do you have method to implement the idea in at the top of the question?
– maxwell
Nov 11 at 20:46
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
for 2nd you can give default css to all images and at a time one image should be active, others will take default css like usetranslate
to in big amount. active image will be visible.
– Jaisa Ram
Nov 11 at 21:03
I want to always show 3 images of 6, The user can swipe to the left and right to show one and hide another per time
– maxwell
Nov 11 at 21:10
@maxwell initially all 3 images should have active and all others should default css. Add and remove class according to your swipe left or right
– Jaisa Ram
Nov 11 at 21:14
|
show 1 more comment
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53252163%2fcreating-simple-pure-javascript-slider-on-swipe-for-mobile-devices%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
So the problem in your case is in this condition if (ImageIndex < items.length) {
. You have total amount of 3 elements, therefore when ImageIndex
is 2, it will increment it to 3
and as you have 3 images in the html items[ImageIndex]
will return undefined.
So change the condition to if (ImageIndex < items.length - 1) {
.
You are right that fixed the error, But how to implement the 2 and half images idea?
– maxwell
Nov 11 at 19:22
What should be a desired behavior, when in the initial state the user swipes left. It should move to the second image or the third one?
– Oleksandr Fedotov
Nov 11 at 19:33
There are 6 images right now, I want 1 of the 2 following cases: 1- 2 images are fully displayed and part of the 3rd image and the last 3 images are hidden, When the user swipes left the hidden parts of the 3rd image would be shown and as swiping the other 3 images would be shown too, And for sure if the user swiped to the left he could swipe to the right again
– maxwell
Nov 11 at 20:42
Case 2: 3 images/1 image are/is shown and the user can swipe to show the 3 others as he is swiping.
– maxwell
Nov 11 at 20:43
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
|
show 1 more comment
So the problem in your case is in this condition if (ImageIndex < items.length) {
. You have total amount of 3 elements, therefore when ImageIndex
is 2, it will increment it to 3
and as you have 3 images in the html items[ImageIndex]
will return undefined.
So change the condition to if (ImageIndex < items.length - 1) {
.
You are right that fixed the error, But how to implement the 2 and half images idea?
– maxwell
Nov 11 at 19:22
What should be a desired behavior, when in the initial state the user swipes left. It should move to the second image or the third one?
– Oleksandr Fedotov
Nov 11 at 19:33
There are 6 images right now, I want 1 of the 2 following cases: 1- 2 images are fully displayed and part of the 3rd image and the last 3 images are hidden, When the user swipes left the hidden parts of the 3rd image would be shown and as swiping the other 3 images would be shown too, And for sure if the user swiped to the left he could swipe to the right again
– maxwell
Nov 11 at 20:42
Case 2: 3 images/1 image are/is shown and the user can swipe to show the 3 others as he is swiping.
– maxwell
Nov 11 at 20:43
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
|
show 1 more comment
So the problem in your case is in this condition if (ImageIndex < items.length) {
. You have total amount of 3 elements, therefore when ImageIndex
is 2, it will increment it to 3
and as you have 3 images in the html items[ImageIndex]
will return undefined.
So change the condition to if (ImageIndex < items.length - 1) {
.
So the problem in your case is in this condition if (ImageIndex < items.length) {
. You have total amount of 3 elements, therefore when ImageIndex
is 2, it will increment it to 3
and as you have 3 images in the html items[ImageIndex]
will return undefined.
So change the condition to if (ImageIndex < items.length - 1) {
.
answered Nov 11 at 19:08
Oleksandr Fedotov
23515
23515
You are right that fixed the error, But how to implement the 2 and half images idea?
– maxwell
Nov 11 at 19:22
What should be a desired behavior, when in the initial state the user swipes left. It should move to the second image or the third one?
– Oleksandr Fedotov
Nov 11 at 19:33
There are 6 images right now, I want 1 of the 2 following cases: 1- 2 images are fully displayed and part of the 3rd image and the last 3 images are hidden, When the user swipes left the hidden parts of the 3rd image would be shown and as swiping the other 3 images would be shown too, And for sure if the user swiped to the left he could swipe to the right again
– maxwell
Nov 11 at 20:42
Case 2: 3 images/1 image are/is shown and the user can swipe to show the 3 others as he is swiping.
– maxwell
Nov 11 at 20:43
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
|
show 1 more comment
You are right that fixed the error, But how to implement the 2 and half images idea?
– maxwell
Nov 11 at 19:22
What should be a desired behavior, when in the initial state the user swipes left. It should move to the second image or the third one?
– Oleksandr Fedotov
Nov 11 at 19:33
There are 6 images right now, I want 1 of the 2 following cases: 1- 2 images are fully displayed and part of the 3rd image and the last 3 images are hidden, When the user swipes left the hidden parts of the 3rd image would be shown and as swiping the other 3 images would be shown too, And for sure if the user swiped to the left he could swipe to the right again
– maxwell
Nov 11 at 20:42
Case 2: 3 images/1 image are/is shown and the user can swipe to show the 3 others as he is swiping.
– maxwell
Nov 11 at 20:43
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
You are right that fixed the error, But how to implement the 2 and half images idea?
– maxwell
Nov 11 at 19:22
You are right that fixed the error, But how to implement the 2 and half images idea?
– maxwell
Nov 11 at 19:22
What should be a desired behavior, when in the initial state the user swipes left. It should move to the second image or the third one?
– Oleksandr Fedotov
Nov 11 at 19:33
What should be a desired behavior, when in the initial state the user swipes left. It should move to the second image or the third one?
– Oleksandr Fedotov
Nov 11 at 19:33
There are 6 images right now, I want 1 of the 2 following cases: 1- 2 images are fully displayed and part of the 3rd image and the last 3 images are hidden, When the user swipes left the hidden parts of the 3rd image would be shown and as swiping the other 3 images would be shown too, And for sure if the user swiped to the left he could swipe to the right again
– maxwell
Nov 11 at 20:42
There are 6 images right now, I want 1 of the 2 following cases: 1- 2 images are fully displayed and part of the 3rd image and the last 3 images are hidden, When the user swipes left the hidden parts of the 3rd image would be shown and as swiping the other 3 images would be shown too, And for sure if the user swiped to the left he could swipe to the right again
– maxwell
Nov 11 at 20:42
Case 2: 3 images/1 image are/is shown and the user can swipe to show the 3 others as he is swiping.
– maxwell
Nov 11 at 20:43
Case 2: 3 images/1 image are/is shown and the user can swipe to show the 3 others as he is swiping.
– maxwell
Nov 11 at 20:43
One more thing, When I swipe the images to the left, The 3 images containers take
margin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main container images-gallary
is visible– maxwell
Nov 11 at 20:51
One more thing, When I swipe the images to the left, The 3 images containers take
margin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main container images-gallary
is visible– maxwell
Nov 11 at 20:51
|
show 1 more comment
Replace your first condition ImageIndex < items.length
with ImageIndex < (items.length - 1)
because total images length is 3 here and trying to access style of items[3]
(undefined
)
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else {
ImageIndex = 0;
}
and one more thing in 2nd condition you should have replace ImageIndex = 0;
with ImageIndex = items.length - 1;
in else condition because always initialising same value ImageIndex = 0;
in case of px > midpoint
and ImageIndex < 1
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else {
ImageIndex = items.length-1;
}
your final code seems like below:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = items.length - 1;
}
}
}
Thanks I updated it, Do you have method to implement the idea in at the top of the question?
– maxwell
Nov 11 at 20:46
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
for 2nd you can give default css to all images and at a time one image should be active, others will take default css like usetranslate
to in big amount. active image will be visible.
– Jaisa Ram
Nov 11 at 21:03
I want to always show 3 images of 6, The user can swipe to the left and right to show one and hide another per time
– maxwell
Nov 11 at 21:10
@maxwell initially all 3 images should have active and all others should default css. Add and remove class according to your swipe left or right
– Jaisa Ram
Nov 11 at 21:14
|
show 1 more comment
Replace your first condition ImageIndex < items.length
with ImageIndex < (items.length - 1)
because total images length is 3 here and trying to access style of items[3]
(undefined
)
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else {
ImageIndex = 0;
}
and one more thing in 2nd condition you should have replace ImageIndex = 0;
with ImageIndex = items.length - 1;
in else condition because always initialising same value ImageIndex = 0;
in case of px > midpoint
and ImageIndex < 1
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else {
ImageIndex = items.length-1;
}
your final code seems like below:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = items.length - 1;
}
}
}
Thanks I updated it, Do you have method to implement the idea in at the top of the question?
– maxwell
Nov 11 at 20:46
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
for 2nd you can give default css to all images and at a time one image should be active, others will take default css like usetranslate
to in big amount. active image will be visible.
– Jaisa Ram
Nov 11 at 21:03
I want to always show 3 images of 6, The user can swipe to the left and right to show one and hide another per time
– maxwell
Nov 11 at 21:10
@maxwell initially all 3 images should have active and all others should default css. Add and remove class according to your swipe left or right
– Jaisa Ram
Nov 11 at 21:14
|
show 1 more comment
Replace your first condition ImageIndex < items.length
with ImageIndex < (items.length - 1)
because total images length is 3 here and trying to access style of items[3]
(undefined
)
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else {
ImageIndex = 0;
}
and one more thing in 2nd condition you should have replace ImageIndex = 0;
with ImageIndex = items.length - 1;
in else condition because always initialising same value ImageIndex = 0;
in case of px > midpoint
and ImageIndex < 1
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else {
ImageIndex = items.length-1;
}
your final code seems like below:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = items.length - 1;
}
}
}
Replace your first condition ImageIndex < items.length
with ImageIndex < (items.length - 1)
because total images length is 3 here and trying to access style of items[3]
(undefined
)
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else {
ImageIndex = 0;
}
and one more thing in 2nd condition you should have replace ImageIndex = 0;
with ImageIndex = items.length - 1;
in else condition because always initialising same value ImageIndex = 0;
in case of px > midpoint
and ImageIndex < 1
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else {
ImageIndex = items.length-1;
}
your final code seems like below:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = items.length - 1;
}
}
}
answered Nov 11 at 19:55
Jaisa Ram
21137
21137
Thanks I updated it, Do you have method to implement the idea in at the top of the question?
– maxwell
Nov 11 at 20:46
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
for 2nd you can give default css to all images and at a time one image should be active, others will take default css like usetranslate
to in big amount. active image will be visible.
– Jaisa Ram
Nov 11 at 21:03
I want to always show 3 images of 6, The user can swipe to the left and right to show one and hide another per time
– maxwell
Nov 11 at 21:10
@maxwell initially all 3 images should have active and all others should default css. Add and remove class according to your swipe left or right
– Jaisa Ram
Nov 11 at 21:14
|
show 1 more comment
Thanks I updated it, Do you have method to implement the idea in at the top of the question?
– maxwell
Nov 11 at 20:46
One more thing, When I swipe the images to the left, The 3 images containers takemargin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main containerimages-gallary
is visible
– maxwell
Nov 11 at 20:51
for 2nd you can give default css to all images and at a time one image should be active, others will take default css like usetranslate
to in big amount. active image will be visible.
– Jaisa Ram
Nov 11 at 21:03
I want to always show 3 images of 6, The user can swipe to the left and right to show one and hide another per time
– maxwell
Nov 11 at 21:10
@maxwell initially all 3 images should have active and all others should default css. Add and remove class according to your swipe left or right
– Jaisa Ram
Nov 11 at 21:14
Thanks I updated it, Do you have method to implement the idea in at the top of the question?
– maxwell
Nov 11 at 20:46
Thanks I updated it, Do you have method to implement the idea in at the top of the question?
– maxwell
Nov 11 at 20:46
One more thing, When I swipe the images to the left, The 3 images containers take
margin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main container images-gallary
is visible– maxwell
Nov 11 at 20:51
One more thing, When I swipe the images to the left, The 3 images containers take
margin-left: -100%
, So when I swipe the first image only the 3 images are moved to out of the screen and only the main container images-gallary
is visible– maxwell
Nov 11 at 20:51
for 2nd you can give default css to all images and at a time one image should be active, others will take default css like use
translate
to in big amount. active image will be visible.– Jaisa Ram
Nov 11 at 21:03
for 2nd you can give default css to all images and at a time one image should be active, others will take default css like use
translate
to in big amount. active image will be visible.– Jaisa Ram
Nov 11 at 21:03
I want to always show 3 images of 6, The user can swipe to the left and right to show one and hide another per time
– maxwell
Nov 11 at 21:10
I want to always show 3 images of 6, The user can swipe to the left and right to show one and hide another per time
– maxwell
Nov 11 at 21:10
@maxwell initially all 3 images should have active and all others should default css. Add and remove class according to your swipe left or right
– Jaisa Ram
Nov 11 at 21:14
@maxwell initially all 3 images should have active and all others should default css. Add and remove class according to your swipe left or right
– Jaisa Ram
Nov 11 at 21:14
|
show 1 more comment
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.
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%2f53252163%2fcreating-simple-pure-javascript-slider-on-swipe-for-mobile-devices%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