Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Make product-image's height also responsive according to image's width

jxltom4a508cefe7a1
diff --git a/saleor/static/js/components/misc.js b/saleor/static/js/components/misc.js
index 6b9abf0037..00c3c4630a 100644
--- a/saleor/static/js/components/misc.js
+++ b/saleor/static/js/components/misc.js
@@ -29,4 +29,29 @@ export default $(document).ready((e) => {
// Preload all images
window.lazySizesConfig.preloadAfterLoad = true;
+
+ // Function for update product image height
+ function updateProductImageHeight() {
+ let productImageWidth = $('.product-image').width();
+ $('.product-image').css({'height': productImageWidth + 'px'});
+ }
+
+ // Update product-image height when window is initialized
+ updateProductImageHeight();
+
+ // Only update product-image height after 5ms when window size changes
+ // This is to prevent window resize event is triggered multiple times at the same time.
+ // This can also prevent that window is resized but bootstrap's responsive component
+ // is not quick enough to resize too.
+ $(window).resize(function() {
+ if (this._timeout) clearTimeout(this._timeout);
+ this._timeout = setTimeout(function() {
+ $(this).trigger('resized');
+ }, 5);
+ });
+
+ // Update product-image height
+ $(window).bind('resized', function() {
+ updateProductImageHeight();
+ });
});
diff --git a/templates/product/_items.html b/templates/product/_items.html
index 7070b35b57..317a8346fd 100644
--- a/templates/product/_items.html
+++ b/templates/product/_items.html
@@ -9,7 +9,7 @@
<a href="{{ product.get_absolute_url }}" class="link--clean">
<div class="text-center">
<div>
- <div class="product-image" style="height: 255px;">
+ <div class="product-image">
<img class="img-responsive lazyload lazypreload"
data-src="{% get_thumbnail product.get_first_image method="thumbnail" size=255 %}"
data-srcset="{% get_thumbnail product.get_first_image method="thumbnail" size=255 %} 1x, {% get_thumbnail product.get_first_image method="thumbnail" size=510 %} 2x"
diff --git a/templates/product/details.html b/templates/product/details.html
index 5dae5ab7c2..b23199c4a0 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -73,7 +73,7 @@
<div class="carousel-inner" role="listbox">
{% for image in images %}
<div class="carousel-item{% if forloop.first %} active{% endif %}">
- <div class="product-image" style="height: 540px;">
+ <div class="product-image">
<img class="d-block img-fluid lazyload lazypreload"
data-src="{% get_thumbnail image.image method="thumbnail" size=540 %}"
data-srcset="{% get_thumbnail image.image method="thumbnail" size=540 %} 1x, {% get_thumbnail image.image method="thumbnail" size=1080 %} 2x"