Skip to content

Instantly share code, notes, and snippets.

@webnitros
Created January 25, 2025 22:29
Show Gist options
  • Save webnitros/7f32d1d2c72e7e562299da7bb3938305 to your computer and use it in GitHub Desktop.
Save webnitros/7f32d1d2c72e7e562299da7bb3938305 to your computer and use it in GitHub Desktop.
Получение близких изображений
<script>
var productId = '[[*id]]'
$(document).ready(function () {
$.getJSON("https://s3.nitronix.ru/close/catalog/2bde8412-c1c5-4e25-8cb7-37e9db00101c/" + productId + ".json", function (data) {
var closes = data.closes; // Получаем список объектов закрытий
var $container = $('#closes'); // Находим контейнер для вставки
if ($container.length > 0) {
var maxImages = 4; // Максимальное количество изображений
// Для каждого объекта в "closes" создаем div с изображением и ссылкой
var displayedImages = 0; // Счётчик отображённых изображений
closes.forEach(function (closeItem) {
if (closeItem.probability >= 60 && displayedImages < maxImages) {
var $div = $('<div></div>').addClass('close-item'); // Создаем новый div с классом
// Создаем ссылку на продукт
var $link = $('<a></a>').attr('href', '/?id=' + closeItem.offer_id)
.attr('title', 'Вероятность схожести '+closeItem.probability);
// Создаем элемент img с ссылкой на изображение
var $img = $('<img />').attr('src', closeItem.picture) // Ссылка на изображение
.attr('alt', 'close image') // Альтернативный текст
.addClass('close-image'); // Класс для img
// Вставляем img в ссылку, а ссылку в div
$link.append($img);
$div.append($link);
// Добавляем div в контейнер
$container.append($div);
displayedImages++;
}
});
if (displayedImages > 1) {
$('#closes_container').show()
}
}
}).fail(function () {
//console.error('Не удалось загрузить данные.');
});
});
</script>
<div id="closes_container" style="display: none">
<h4>Похожие товары</h4>
<div id="closes"></div>
</div>
<style>
#closes_container {
margin-top: 50px;
}
#closes img {
max-width: 90px;
max-height: 90px;
}
.close-item {
display: inline-block;
margin-left: 10px;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment