Skip to content

Instantly share code, notes, and snippets.

@Buntelrus
Created August 5, 2020 18:30
Show Gist options
  • Save Buntelrus/1e27308f0f7fe25ab4f1f3e71834f35c to your computer and use it in GitHub Desktop.
Save Buntelrus/1e27308f0f7fe25ab4f1f3e71834f35c to your computer and use it in GitHub Desktop.
Improvement for https://erripis.gr/Beirut/
/**
* This prepares html
* You can adjust your html directly and then delete this code block.
*/
//create container element which will have the exact width of its content (image)
const container = document.createElement('div')
//get your slider element
let slider = document.querySelector('.beforeAfterSlidebar')
//override position absolute (you need 1 image in dom flow for the container to set width properly)
slider.children[0].style.position = 'initial'
//append children to container element
let child
while (child = slider.firstChild) {
container.append(child)
}
//append container to your slider
slider.append(container)
/**
* Use this in your javascript
*/
//set your slider element
slider = container
//define event handler
function moveSlider(event) {
const offsetX = event.offsetX !== undefined? event.offsetX : event.touches[0].clientX
slider.querySelector('.topImage').style.width = `${offsetX}px`
}
//attach event listener
slider.addEventListener('mousemove', moveSlider)
slider.addEventListener('touchmove', moveSlider)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment