Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
10 changes: 5 additions & 5 deletions example/showcase.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
</div>
</header>
<main>
<div class="album">
<div class="album">
<div class="img-container">
<img
<img id="imgtest"
class="hover-preview img-preview img-1"
src=""
style="border-radius:50%"
Expand All @@ -48,7 +48,7 @@ <h2>Use is as a preview of a gallery</h2>
</div>
</div>

<div class="album">
<div class="album">
<div class="info">
<h2>Or as a preview for a video</h2>
<p>
Expand All @@ -63,9 +63,9 @@ <h2>Or as a preview for a video</h2>
</div>
</div>

<div class="album">
<div class="album">
<div class="img-container">
<img
<img
class="hover-preview img-preview img-3 "
src=""
style="border-radius:20%"
Expand Down
44 changes: 43 additions & 1 deletion src/hover-preview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
const HOVER_DELAY = 70
const PREVIEW_DURATION = 500
const images = Array.from(document.querySelectorAll('.hover-preview'))
const images = Array.from(document.querySelectorAll('img.hover-preview'))

// singleton timeout object, only one timeout should be active at a time
const timeout = {
Expand Down Expand Up @@ -90,4 +90,46 @@
images.forEach(img => {
img.addEventListener('mouseenter', mouseEnterListener)
})


const almover = () => {
let last_known_scroll_position = window.scrollY;
images.forEach(img => {

if((last_known_scroll_position >= (parseInt(img.dataset.posy) - 10)) && (last_known_scroll_position <= (parseInt(img.dataset.posy) + 10)))
{

hoveredImg.el = img
hoveredImg.poster = hoveredImg.el.getAttribute('src')
hoveredImg.index = -1
hoveredImg.images = hoveredImg.el.getAttribute('data-preview').split('|')

timeout.set(() => previewImages(true), HOVER_DELAY)
hoveredImg.el.addEventListener('mouseleave', () => {
hoveredImg.reset()
timeout.reset()
})

//console.log(img.id);
//mouseEnterListener(img);
//console.log(`${img.id}, img position :${img.dataset.posy} , scroll positions: ${last_known_scroll_position}`);
}
})
}

const setPosYImages = () => {
images.forEach(img => {
img.dataset.posy = img.getBoundingClientRect().y;
})
}

window.addEventListener('scroll', function(){
almover();
})

window.addEventListener('load', function(){
setPosYImages();
})


})()