-1

I'm redesigning a confluence page that displays various visualizations. I am using the following code:

<div class="carousel">
     <img src="image1.jpg">
     <img src="image2.jpg">
     <img src="image3.jpg">
</div>

CSS (within a style tag):

.carousel {
     display: flex;
     overflow-x: scroll;
     scroll-snap-type: x mandatory;
     scroll-behavior: smooth;
}
.carousel img {
      width: 100%;
      scroll-snap-align: start;
}

javascript (within a script tag):

const carousel = document.querySelector(".carousel");

let isDown = false;
let startX;
let scrollLeft;

carousel.addEventListener("mousedown", (e) => {
     isDown = true;
     startX = e.pageX - carousel.offsetLeft;
     scrollLeft = carousel.scrollLeft;
});
carousel.addEventListener("mouseleave", () => {
      isDown = false;
});
carousel.addEventListener("mouseup", () => {
     isDown = false;
});
carousel.addEventListener("mousemove", (e) => {
     if (!isDown) return;
     e.preventDefault();
     const x = e.pageX - carousel.offsetLeft;
     const walk = x - startX;
     carousel.scrollLeft = scrollLeft - walk;
});

I have tried using the anchor tag with the href surrounding each image, but it doesn't seem to work within the macro or perhaps the carousel div itself, but I'm totally willing to concede that the CSS or javascript may be the issue here.

3
  • Not sure why wrapping the image in anchors wouldn't work? Can you provide an minimal reproducible example? Commented Jul 17, 2024 at 14:39
  • What "doesn't seem to work" about it? Can you update the question with a minimal reproducible example as a runnable code snippet which demonstrates the problem? Commented Jul 17, 2024 at 14:42
  • And please use snippet! Commented Jul 17, 2024 at 14:49

1 Answer 1

0

you will need to adjust the css to match the change in structure when you wrap the images in anchors.

const carousel = document.querySelector(".carousel");

let isDown = false;
let startX;
let scrollLeft;

carousel.addEventListener("mousedown", (e) => {
     isDown = true;
     startX = e.pageX - carousel.offsetLeft;
     scrollLeft = carousel.scrollLeft;
});
carousel.addEventListener("mouseleave", () => {
      isDown = false;
});
carousel.addEventListener("mouseup", () => {
     isDown = false;
});
carousel.addEventListener("mousemove", (e) => {
     if (!isDown) return;
     e.preventDefault();
     const x = e.pageX - carousel.offsetLeft;
     const walk = x - startX;
     carousel.scrollLeft = scrollLeft - walk;
});
.carousel {
     display: flex;
     overflow-x: scroll;
     scroll-snap-type: x mandatory;
     scroll-behavior: smooth;
}
.carousel a {
      width: 100%;
      scroll-snap-align: start;
}
<div class="carousel">
     <a><img src="image1.jpg"></a>
     <a><img src="image2.jpg"></a>
     <a><img src="image3.jpg"></a>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.