Hey friends,
I’ve been working with a horizontal scrolling idea recently and I thought it would be cool to add buttons that take you to each picture. Has anyone got any ideas on how to do this? My js so far has got me pretty close. My first block of code gets me close, but when I change the browser window size the childWidth variable doesn’t change alongside. Which means the scroll movement is out of place… It works well without changing window size…
In the second block of code, I’ve began making in roads into making the div length responsive. But I’m not sure how to feed that value into the final button click event…
Any help is greatly appreciated as always!
Code 1
// x = horizontal
// y = vertical
let browserWindowY = window.innerHeight;
let browserWindowX = window.innerWidth;
const container = document.querySelector('.wrapper');
const items = document.querySelectorAll('.item')
let childWidth = items[0].clientWidth;
function assignWindowSize() {
container.style.width = window.innerWidth + 'px';
container.style.height = window.innerHeight + 'px';
items.forEach(e => e.style.width = window.innerWidth + 'px');
items.forEach(e => e.style.height = window.innerHeight + 'px');
}
window.onresize = assignWindowSize;
// Button to next/last picture
// button selector
let nextPictureButton = document.querySelectorAll('.next-picture');
let lastPictureButton = document.querySelectorAll('.last-picture');
// starting horizontal scroll coordinate
let scrollPoint = 0;
nextPictureButton.forEach(e => e.addEventListener('click', nextPic));
lastPictureButton.forEach(e => e.addEventListener('click', lastPic));
console.log(scrollPoint)
function nextPic() {
container.scrollLeft = scrollPoint += childWidth;
}
function lastPic() {
container.scrollLeft = scrollPoint -= childWidth;
}
Code 2
// x = horizontal
// y = vertical
let browserWindowY = window.innerHeight;
let browserWindowX = window.innerWidth;
const container = document.querySelector('.wrapper');
const items = document.querySelectorAll('.item')
function assignWindowSize() {
container.style.width = window.innerWidth + 'px';
container.style.height = window.innerHeight + 'px';
items.forEach(e => e.style.width = window.innerWidth + 'px');
items.forEach(e => e.style.height = window.innerHeight + 'px');
// measure child width and feed into button click events
let childWidthStart = 0;
let childWidth = items[0].clientWidth;
childWidthStart + childWidth;
console.log(childWidthStart + childWidth)
}
window.onresize = assignWindowSize;
// Button to next/last picture
// button selector
let nextPictureButton = document.querySelectorAll('.next-picture');
let lastPictureButton = document.querySelectorAll('.last-picture');
// starting horizontal scroll coordinate
let scrollPoint = 0;
nextPictureButton.forEach(e => e.addEventListener('click', nextPic));
lastPictureButton.forEach(e => e.addEventListener('click', lastPic));
console.log(scrollPoint)
function nextPic() {
container.scrollLeft = scrollPoint += childWidth;
}
function lastPic() {
container.scrollLeft = scrollPoint -= childWidth;
}