I want to create image carousel in my GitHub README.md file. I saw that code HTML does not work very well in markdown but I want to know if it is possible to do carousel in markdown. I am using HTML code for image slider from here
I want to create image carousel in my GitHub README.md file. I saw that code HTML does not work very well in markdown but I want to know if it is possible to do carousel in markdown. I am using HTML code for image slider from here
You can do anything with markdown. You just need some code. You can add a javascript code in your mkdocs.yml
extra_javascript:
- js/image-carousel.js
Here is a very simple mkdocs Carousel that search all elements in page and turn sequenced images with same rect size in a carousel.
// $ cat docs/js/image-carousel.js
var keepTime = 2000;
console.log("Carousel MKDocs");
function updateCarousel(img) {
if (img.carouselRunning) {
let outstyle = img.carousel[img.carouselIndex % img.carousel.length].style;
outstyle.visibility = 'hidden';
outstyle.opacity = 0;
img.carouselIndex = (img.carouselIndex + 1) % img.carousel.length;
let instyle = img.carousel[img.carouselIndex % img.carousel.length].style;
instyle.visibility = 'visible';
instyle.opacity = 1;
instyle.position = 'absolute';
}
setTimeout(updateCarousel, keepTime, img);
}
function setCarouselEvents(img) {
img.style.visibility = 'hidden';
img.style.transition = 'opacity 1.3s, visibility 1.3s';
img.style.position = 'absolute';
img.addEventListener(
'mouseover', function(e) { this.carousel[0].carouselRunning = false; });
img.addEventListener(
'mouseout', function(e) { this.carousel[0].carouselRunning = true; });
}
function setCarousel(img) {
img.carouselRunning = true;
setCarouselEvents(img);
img.carouselIndex = 0;
setTimeout(updateCarousel, 1, img);
}
// fist we need to ask DOM for all p > img tags
let imgs = document.querySelectorAll('P > IMG');
for (let i = 1; i < imgs.length; i++) {
let h = imgs[i].naturalHeight;
let w = imgs[i].naturalWidth;
let pe = imgs[i].previousElementSibling;
if (!pe) {
continue;
}
if (pe.nodeName != "IMG") {
continue;
}
let sh = pe.naturalHeight;
let sw = pe.naturalWidth;
if (sw != w || sh != h) {
continue;
}
if (imgs[i].carousel) {
continue;
}
if (!pe.carousel) {
pe.carousel = [ pe ];
setCarousel(pe);
}
pe.carousel.push(imgs[i]);
imgs[i].carousel = pe.carousel;
setCarouselEvents(imgs[i]);
// set parent size
pe.parentElement.style.minWidth = "calc(" + sw + "px + 2em)";
pe.parentElement.style.minHeight = "calc(" + sh + "px + 2em)";
}
This question is a little bit outdated but still relevant, so I want to share my (a little bit hacky) way to do it as a future reference:
I use ffmpeg to create a gif with crossfade
ffmpeg \
-framerate 10 -loop 1 -t 5 -i 1.png \
-framerate 10 -loop 1 -t 5 -i 2.png \
-framerate 10 -loop 1 -t 5 -i 3.png \
-framerate 10 -loop 1 -t 5 -i 4.png \
-framerate 10 -loop 1 -t 5 -i 5.png \
-filter_complex \
"[1]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+4/TB[f0]; \
[2]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+8/TB[f1]; \
[3]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+12/TB[f2]; \
[4]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+16/TB[f3]; \
[0][f0]overlay[bg1];[bg1][f1]overlay[bg2];[bg2][f2]overlay[bg3];[bg3][f3]overlay,split[v0][v1]; \
[v0]palettegen[p];[v1][p]paletteuse[v]" -map "[v]" out.gif
After that I'm going to create an issue on Github "add a slideshow to readme" and here I'm going to add the gif as comment. Now you can simply copy the created link to the github raw content upload and paste it into your readme.
Now you have a slideshow in your readme without adding any images or code to your repo and without using javascript.