Некодим логотипНекодим
МодификацииПубликацииНекодИИмЦены
Некодим логотипНекодим

© 2020-2026 Некодим

Готовые модификации для вашего сайта. Улучшайте функциональность и дизайн без программирования.

Компания

  • Контакты
  • Публичная оферта
  • Политика конфиденциальности
  • Согласие на обработку данных
  • Лицензионное соглашение
  • Реферальная программа

Полезное

  • Модификации
  • Публикации
  • Каталог блоков Tilda
  • MCP для агентов
  • Журнал изменений
  • Доска почёта

Личный кабинет

  • Вход
  • Регистрация
  • Цены
Журнал измененийИстория GitФайлыTilda MCP
Каталог файлов

tilda-zoom-2.0.js

Код файла
Версия от 04.12.2026, 04:03:54 МСК
Оригинал на CDNСнимок 56c3b845Полный текст
1201		};1202	}1203}1204 1205/**1206 * wait for zoomed image to contain src, natural width and height1207 *1208 * @param {HTMLImageElement} zoomedImage1209 * @param {number} timer - Date.now()1210 * @param {function} callback1211 */1212function t_zoom__waitImgForScale(zoomedImage, timer, callback) {
История файла и сравнение версий
  • 04.12.2026, 04:03:54 · 56c3b845
1213
if (zoomedImage.src && zoomedImage.naturalWidth && zoomedImage.naturalHeight) {
1214 callback();
1215 // wait no more than 3 seconds
1216 } else if (Date.now() - timer < 3000) {
1217 setTimeout(() => {
1218 t_zoom__waitImgForScale(zoomedImage, timer, callback);
1219 }, 500);
1220 } else {
1221 const { naturalWidth, naturalHeight } = zoomedImage;
1222 console.warn(`zoomed image isn't complete, natural width: ${naturalWidth}, natural height: ${naturalHeight}`);
1223 callback();
1224 }
1225}
1226
1227/**
1228 * we cannot check original size of svg, and then we should check, is img has .svg format,
1229 * and if yes, fetch original <svg> to get original height and width
1230 *
1231 * @param {HTMLImageElement} zoomedImage - current zoomed image
1232 */
1233function t_zoom_checkToScaleInit(zoomedImage) {
1234 const windowWidth = window.innerWidth;
1235 const windowHeight = window.innerHeight;
1236 const { isMobile, isDoubleTapActivated } = t_getZoomState();
1237 const zoomerWrapper = document.querySelector('.t-zoomer__wrapper');
1238 zoomerWrapper.classList.remove('zoomer-no-scale');
1239 //prettier-ignore
1240 const hasOriginalSvgParams = zoomedImage.hasAttribute('data-original-svg-height') || zoomedImage.hasAttribute('data-original-svg-width');
1241 const imageHeight = parseInt(zoomedImage.getAttribute('data-original-svg-height'), 10) || zoomedImage.naturalHeight;
1242 const imageWidth = parseInt(zoomedImage.getAttribute('data-original-svg-width'), 10) || zoomedImage.naturalWidth;
1243
1244 if (zoomedImage.src.includes('.svg') && !window.isIE && !hasOriginalSvgParams) {
1245 t_zoom_fetchSVG(zoomedImage, windowHeight, windowWidth);
1246 } else if (windowHeight < imageHeight || windowWidth < imageWidth) {
1247 if (!isDoubleTapActivated && isMobile) t_zoom_doubletapScaleInit();
1248 t_zoom_scale_init();
1249 } else {
1250 document.querySelector('.t-zoomer__scale').classList.add('t-zoomer__scale--disabled');
1251 zoomerWrapper.classList.add('zoomer-no-scale');
1252 }
1253}
1254
1255/**
1256 * get <svg> to check it real size
1257 *
1258 * @param {HTMLImageElement} zoomedImage - current image
1259 * @param {number} windowHeight
1260 * @param {number} windowWidth
1261 */
1262function t_zoom_fetchSVG(zoomedImage, windowHeight, windowWidth) {
1263 const url = zoomedImage.src;
1264 const { isMobile, isDoubleTapActivated } = t_getZoomState();
1265 fetch(url)
1266 .then(response => response.text())
1267 .then(svg => {
1268 const div = document.createElement('div');
1269 document.body.insertAdjacentElement('beforeend', div);
1270 div.innerHTML = svg;
1271 const parsedSVG = div.querySelector('svg');
1272 let parsedSVGWidth = Math.round(parsedSVG.getBBox().width);
1273 let parsedSVGHeight = Math.round(parsedSVG.getBBox().height);
1274 const svgRatio = parsedSVGWidth / parsedSVGHeight;
1275
1276 const svgLimitWidth = 5000;
1277 if (parsedSVGWidth > svgLimitWidth) {
1278 parsedSVGWidth = svgLimitWidth;
1279 parsedSVGHeight = Math.round(parsedSVGWidth / svgRatio);
1280 }
1281
1282 zoomedImage.setAttribute('data-original-svg-width', parsedSVGWidth);
1283 zoomedImage.setAttribute('data-original-svg-height', parsedSVGHeight);
1284
1285 const isSvgLargerThanWindow = windowHeight < parsedSVGHeight || windowWidth < parsedSVGWidth;
1286
1287 // Если у SVG не указаны атрибуты с размерами, то без явных размеров он займет размеры контейнера (0, 0)
1288 const attrWidth = parsedSVG.getAttribute('width');
1289 const attrHeight = parsedSVG.getAttribute('height');
1290 const hasDefinedSize = attrWidth || attrHeight;
1291 if (!hasDefinedSize || isSvgLargerThanWindow) {
1292 zoomedImage.style.width = `${parsedSVGWidth}px`;
1293 zoomedImage.style.height = `${parsedSVGHeight}px`;
1294 }
1295
1296 if (isSvgLargerThanWindow) {
1297 if (!isDoubleTapActivated && isMobile) t_zoom_doubletapScaleInit();
1298 t_zoom_scale_init();
1299 } else {
1300 document.querySelector('.t-zoomer__scale').classList.add('t-zoomer__scale--disabled');
Предыдущие 100 строкСледующие 100 строк