Link to home
Start Free TrialLog in
Avatar of B O
B O

asked on

I have a slide function that slides between two images, and show the before and after, the issue is that the looped out divs containing the images doenst slide with the range inpt that controls the sl

I have a slide function that slides between two images, and show the before and after, the issue is that the looped out divs containing the images doenst slide with the range input that controls the motion between the two images.

document.querySelector(".slider input").addEventListener("input", function(){
  document.querySelector(".original-image").style.width = this.value + "%";
});
 
document.querySelector(".before input").addEventListener("input", function(){
  document.querySelector(".after").style.width = this.value + "%";
});

Open in new window

// img name container
let key = [];
const response = { imgOne: [key], imgTwo: [key]};


// php response data container 
let conData = [];


// creates dynamic div/ slider
function slider(valueOne, valueTwo){    
    let objSli = `<div class="slider" class="before" style="background-image: url('/blog-app/img/compare/${valueOne}')">
    <div class="original-image" class="after" style="background-image: url('/blog-app/img/compare/${valueTwo}')">
        </div>
    <input type="range" value="50" max="100">
</div>
<br>`;
return objSli;
};




function pushImgName(i){
    response.imgOne[i] = new Array();
    response.imgTwo[i] = new Array();
    response.imgOne[i] += conData[i]["imgName1"];
    response.imgTwo[i] += conData[i]["imgName2"];
}
   
$.ajax({
    url: '/blog-app/includes/compare_thumbnails.incl.php',
    type: 'POST',
    success: function(data) {
            //parse out the data in JSON form
            console.log(data, typeof data);
            // console.log(data);
            conData = JSON.parse(data);
            if (!data.error){
                for (let i = 0; i < conData.length; i++){
                pushImgName(i);   
                }
            //show var response
            console.log(response);       
            }
            $.get("/blog-app/includes/compare_thumbnails.incl.php", {img: response}, function(base64) {
                const respObj = Array(response);    
                respObj.forEach(element => {
                    for (let i = 0; i < element["imgOne"].length; i++){
                        $("#result").append(slider(element["imgOne"][i], element["imgTwo"][i])); 
                    }
                });
             });
    }
});

Open in new window


body{
  margin: 0;
  padding: 0;
}


.slider{
  width: 420px;
  height: 350px;
  display: flex;
  background: url("https://www.nielsenhaarkliniek.nl/wp-content/uploads/2019/10/Karel-Del-casteleto-Na.jpeg") no-repeat;
  background-position: 0 15%;
  background-size: 40vw;
  position: relative;
  object-fit: center;
}


.slider input{
  -webkit-appearance: none;
  width: 100%;
  background: none;
  z-index: 10;
}


.slider input::-webkit-slider-thumb{
  -webkit-appearance: none;
  width: 10px;
  height: 350px;
  background-color: #111;
  cursor: pointer;
}


.original-image{
  position: absolute;
  left: 0;
  width: 50%;
  height: 350px;
  background: url("https://www.nielsenhaarkliniek.nl/wp-content/uploads/2019/10/Karel-Del-casteleto-Voor.jpeg") no-repeat;
  background-position: 0 15%;
  background-size: 40vw;
  object-fit: center;
}

Open in new window

This one is static and works fine


User generated image
These ones are dynamic and the slide function doesnt work at allUser generated image

Avatar of HainKurt
HainKurt
Flag of Canada image

can you create a test page on your server to test this

you are getting some js errors
User generated imagecan you click on the link here "youtube_api.js:3" and see what code here is...
Avatar of B O
B O

ASKER

Alright I could try that, but isnt it suppose to work even if its local?

The youtube_api.js is a script that belongs to a different page.
I got all my scripts at the footer.php right before the end of the body tag.

User generated image
Avatar of B O

ASKER

This is what compare.js says

User generated image
Alright I could try that, but isnt it suppose to work even if its local?

yes, but we cannot see whats going on by just looking at screenshots :)
you have thıs error

User generated imageprobably unrelated, or some artifact from some tools that are removed...
page does not have any element with id=search
also you do not have any element with class "before"


User generated image
did you forgot to add those classes?
maybe you mean these elements...

User generated image
Avatar of B O

ASKER

Ohe that weird this is how my dynamic slider look like

// creates dynamic div/ slider
function slider(valueOne, valueTwo){    
    let objSli = `<div class="slider" class="before" style="background-image: url('/blog-app/img/compare/${valueOne}')">
    <div class="original-image" class="after" style="background-image: url('/blog-app/img/compare/${valueTwo}')">
        </div>
    <input type="range" value="50" max="100">
</div>
<br>`;
return objSli;
};

Open in new window

Avatar of B O

ASKER

Ohe now I see it double :D
Avatar of B O

ASKER

I have edited the code but is still didnt fixx the issue
ok, the issue is you do not bind the events on new ones, retrieved by ajax

document.querySelector(".slider input").addEventListener("input", function(){
  document.querySelector(".original-image").style.width = this.value + "%";
});
 
document.querySelector(".before input").addEventListener("input", function(){
  document.querySelector(".after").style.width = this.value + "%";
});

Open in new window

this is called for elements on page, but the ones that comes from ajax are not involved here...
somehow you should call these inside ajax/success events...
not before any element is retrieved, which you do now, and gives error on load...
this is added by ajax
User generated image
input does not have any "input" event set

this is initial one, and input event is set...
User generated image
so, you have to call
document.querySelector(".before input").addEventListener("input", function(){
  document.querySelector(".after").style.width = this.value + "%";
});

Open in new window

after new one is added,
User generated imageafter line 47 maybe...
Avatar of B O

ASKER

Thank you very much I got the first one working.
But now a lil stuck with making it dynamic,
You can see the link http://teddybotchway.nl/istanbulhair/compare-post.php 
after Line 47, put this

$(".before input").on("input", function(){
  $(this).prev()[0].style.width = this.value + "%";
});

Open in new window

should work
User generated image

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
almost there, use [0] all the time, not [i]
we need first element here all the time...

use this after the loop!

$(".before input").on("input", function(){
  $(this).prev()[0].style.width = this.value + "%";
});

Open in new window

User generated imagefirst append all in the loop, then bind events after the loop, and use [0] not [i]

hope this time you make it work :)
Avatar of B O

ASKER

Thank you very much for your help Hainkurt
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DONE!
no errors now...
:)
Avatar of B O

ASKER

Thank you for still giving advice,
I think its perfect now.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of B O

ASKER

Thank you for still hanging with me :P

I deleted lin 5-7 from compare.js

and changed my youtube_api.js
to this:
const searchField = document.querySelector('#search');          

        if (searchField){
            searchField.addEventListener('input', youtubeApiCall);
        }

Seem to work fine now