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.
These ones are dynamic and the slide function doesnt work at all
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 + "%";
});
// 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]));
}
});
});
}
});
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;
}
This one is static and works fineThese ones are dynamic and the slide function doesnt work at all
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.
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.
ASKER
This is what compare.js says
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 :)
ASKER
you have thıs error
probably unrelated, or some artifact from some tools that are removed...
page does not have any element with id=search
probably 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"
did you forgot to add those classes?
maybe you mean these elements...
did you forgot to add those classes?
maybe you mean these elements...
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;
};
ASKER
Ohe now I see it double :D
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
somehow you should call these inside ajax/success events...
not before any element is retrieved, which you do now, and gives error on load...
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 + "%";
});
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
input does not have any "input" event set
this is initial one, and input event is set...
so, you have to call
after line 47 maybe...
input does not have any "input" event set
this is initial one, and input event is set...
so, you have to call
document.querySelector(".before input").addEventListener("input", function(){
document.querySelector(".after").style.width = this.value + "%";
});
after new one is added,after line 47 maybe...
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
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 + "%";
});
should workASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
almost there, use [0] all the time, not [i]
we need first element here all the time...
use this after the loop!
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 + "%";
});
first append all in the loop, then bind events after the loop, and use [0] not [i]
hope this time you make it work :)
hope this time you make it work :)
ASKER
Thank you very much for your help Hainkurt
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DONE!
no errors now...
:)
no errors now...
:)
ASKER
Thank you for still giving advice,
I think its perfect now.
I think its perfect now.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
you are getting some js errors
can you click on the link here "youtube_api.js:3" and see what code here is...