I've created a timeline using HTML, CSS and JS. I've put the HTML in a custom HTML block on a page in WP, added the CSS to the extra CSS in the editor and added a custom JS block on the page with the JS code.
The HTML and CSS work, but the JS doesn't. I've checked it in codepen, and it works fine.
Do you have any suggestions about what could be wrong here?
Link to the WP Page, link to codepen.
JAVASCRIPT
(function ($) {
$.fn.timeline = function () {
var selectors = {
id: $(this),
item: $(this).find(".timeline-item"),
activeClass: "timeline-item--active",
img: ".timeline__img"
};
selectors.item.eq(0).addClass(selectors.activeClass);
selectors.id.css(
"background-image",
"url(" + selectors.item.first().find(selectors.img).attr("src") + ")"
);
var itemLength = selectors.item.length;
$(window).scroll(function () {
var max, min;
var pos = $(this).scrollTop();
selectors.item.each(function (i) {
min = $(this).offset().top;
max = $(this).height() + $(this).offset().top;
var that = $(this);
if (i == itemLength - 2 && pos > min + $(this).height() / 2) {
selectors.item.removeClass(selectors.activeClass);
selectors.id.css(
"background-image",
"url(" + selectors.item.last().find(selectors.img).attr("src") + ")"
);
selectors.item.last().addClass(selectors.activeClass);
} else if (pos <= max - 40 && pos >= min) {
selectors.id.css(
"background-image",
"url(" + $(this).find(selectors.img).attr("src") + ")"
);
selectors.item.removeClass(selectors.activeClass);
$(this).addClass(selectors.activeClass);
}
});
});
};
})(jQuery);
$("#timeline-1").timeline();
I used WPcode to insert a shortcode on the page, but that didn't work either. I've also used the Wordpen plugin, but not working.