fix(js): improve lightbox WebP fallback error handler
- Use .off('error') to prevent stacking error handlers
- Simplify fallback logic: only replace .webp → .png on error
- Prevents infinite error loops
This commit is contained in:
@@ -46,10 +46,11 @@ $(function () {
|
|||||||
// Lightbox – gallery grid items
|
// Lightbox – gallery grid items
|
||||||
$(document).on("click", ".grid-item", function () {
|
$(document).on("click", ".grid-item", function () {
|
||||||
var src = $(this).data("img") || $(this).find("img").attr("src");
|
var src = $(this).data("img") || $(this).find("img").attr("src");
|
||||||
$("#lightboxImg").on("error", function () {
|
$("#lightboxImg").off("error").on("error", function () {
|
||||||
// WebP fallback: try original format
|
// WebP fallback: try original format
|
||||||
var fallback = src.replace(/\.webp$/, src.endsWith('.webp') ? '.png' : '.jpg');
|
if ($(this).attr('src').endsWith('.webp')) {
|
||||||
if ($(this).attr('src') !== fallback) $(this).attr('src', fallback);
|
$(this).attr('src', src.replace(/\.webp$/, '.png'));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
$("#lightboxImg").attr("src", src);
|
$("#lightboxImg").attr("src", src);
|
||||||
$("#lightbox").addClass("open");
|
$("#lightbox").addClass("open");
|
||||||
@@ -59,9 +60,10 @@ $(function () {
|
|||||||
// Lightbox – floor plan images in Raumaufteilung
|
// Lightbox – floor plan images in Raumaufteilung
|
||||||
$(document).on("click", ".floor-plan img[data-img]", function () {
|
$(document).on("click", ".floor-plan img[data-img]", function () {
|
||||||
var src = $(this).data("img");
|
var src = $(this).data("img");
|
||||||
$("#lightboxImg").on("error", function () {
|
$("#lightboxImg").off("error").on("error", function () {
|
||||||
var fallback = src.replace(/\.webp$/, '.png');
|
if ($(this).attr('src').endsWith('.webp')) {
|
||||||
if ($(this).attr('src') !== fallback) $(this).attr('src', fallback);
|
$(this).attr('src', src.replace(/\.webp$/, '.png'));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
$("#lightboxImg").attr("src", src);
|
$("#lightboxImg").attr("src", src);
|
||||||
$("#lightbox").addClass("open");
|
$("#lightbox").addClass("open");
|
||||||
|
|||||||
Reference in New Issue
Block a user