Last active
March 4, 2020 21:12
-
-
Save cahva/8a7b926e9aa7b12ad929f8719690cb3b to your computer and use it in GitHub Desktop.
Add link to slide using observer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
var slideImgEl; | |
// Change slidename you want to have a link | |
var slideName = '/2.jpg'; | |
// Link URL | |
var linkUrl = 'https://www.skistar.com/sv/skistarliving/kop/are/are-by/brf-are-village-2-85m2-UgE/#broker'; | |
var observer = new MutationObserver(function(mutations) { | |
mutations.forEach(function(mutation) { | |
if (mutation.attributeName === "src") { | |
if (mutation.target.src.indexOf(slideName) !== -1) { | |
slideImgEl.addEventListener('click', openLink); | |
slideImgEl.classList.add("hover-pointer"); | |
} else { | |
slideImgEl.removeEventListener('click', openLink); | |
slideImgEl.classList.remove("hover-pointer"); | |
} | |
} | |
}); | |
}); | |
function openLink() { | |
window.open(linkUrl); | |
} | |
function tryEl(e) { | |
slideImgEl = document.querySelector('.slide-img'); | |
if (!slideImgEl) { | |
window.requestAnimationFrame(tryEl); | |
} else { | |
if (!slideImgEl.src) { | |
window.requestAnimationFrame(tryEl); | |
} else { | |
observer.observe(slideImgEl, { attributes: true }); | |
} | |
} | |
} | |
tryEl(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment