#前端开发# 今天花了整个下午把博客重构了一遍,从 Next.js 迁移到了纯静态 HTML。说实话,有时候最简单的方案反而是最好的——不需要构建工具,不需要依赖管理,打开浏览器就能跑...
" />
主题风格
选择你喜欢的配色方案
🌙 跟随系统暗色
← 返回列表

用AI生成的Jquery的网页图片查看器,自适应移动端

L
📅 2026-03-24 10:13:39 👁 25 次阅读 💬 48 条评论
     

/* ===== Lightbox 样式 ===== */
.lightbox-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.92);
  justify-content: center;
  align-items: center;
  flex-direction: column;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.lightbox-overlay.active {
  display: flex;
  opacity: 1;
}

.lightbox-img {
  max-width: 90vw;
  max-height: 82vh;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
  user-select: none;
  -webkit-user-drag: none;
  transition: transform 0.25s ease;
}

/* 关闭按钮 */
.lightbox-close {
  position: absolute;
  top: 16px;
  right: 20px;
  background: none;
  border: none;
  color: #fff;
  font-size: 36px;
  cursor: pointer;
  z-index: 10;
  line-height: 1;
  opacity: 0.7;
  transition: opacity 0.2s;
  padding: 4px 10px;
}
.lightbox-close:hover { opacity: 1; }

/* 左右切换按钮 */
.lightbox-prev,
.lightbox-next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 40px;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 6px;
  z-index: 10;
  line-height: 1;
  opacity: 0.6;
  transition: opacity 0.2s, background 0.2s;
}
.lightbox-prev:hover,
.lightbox-next:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.15);
}
.lightbox-prev { left: 16px; }
.lightbox-next { right: 16px; }

/* 计数器 */
.lightbox-counter {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
  font-family: 'DM Mono', monospace;
  letter-spacing: 2px;
}

/* ===== 移动端适配 ===== */
@media (max-width: 768px) {
  .lightbox-prev,
  .lightbox-next {
    font-size: 28px;
    padding: 6px 12px;
  }
  .lightbox-prev { left: 8px; }
  .lightbox-next { right: 8px; }
  .lightbox-close {
    top: 10px;
    right: 12px;
    font-size: 30px;
  }
  .lightbox-img {
    max-width: 96vw;
    max-height: 78vh;
  }
  .lightbox-counter { bottom: 14px; }
}

/* 小屏手机隐藏侧边按钮,纯手势操作 */
@media (max-width: 420px) {
  .lightbox-prev,
  .lightbox-next {
    display: none;
  }
}


$(function () {

  var $overlay  = $('.lightbox-overlay');
  var $img      = $('.lightbox-img');
  var $counter  = $('.lightbox-counter');

  // 收集当前页面所有 .post-img img 的 src
  var gallery   = [];
  var curIndex  = 0;

  function collectImages() {
    gallery = [];
    $('.post-img img').each(function () {
      gallery.push($(this).attr('src'));
    });
  }

  function showImage(index) {
    if (index < 0) index = gallery.length - 1; if (index >= gallery.length) index = 0;
    curIndex = index;

    $img.attr('src', gallery[curIndex]);
    $counter.text((curIndex + 1) + ' / ' + gallery.length);
  }

  function openLightbox(index) {
    collectImages();
    showImage(index);
    $overlay.css('display', 'flex');
    // 触发 reflow 后加 class 实现淡入
    $overlay[0].offsetHeight;
    $overlay.addClass('active');
    $('body').css('overflow', 'hidden');
  }

  function closeLightbox() {
    $overlay.removeClass('active');
    setTimeout(function () {
      $overlay.css('display', 'none');
      $img.attr('src', '');
      $('body').css('overflow', '');
    }, 300);
  }

  // ---- 点击图片打开 ----
  $(document).on('click', '.post-img img', function (e) {
    e.stopPropagation();
    var src = $(this).attr('src');
    collectImages();
    var index = gallery.indexOf(src);
    if (index === -1) index = 0;
    openLightbox(index);
  });

  // ---- 关闭 ----
  $('.lightbox-close').on('click', closeLightbox);

  // 点击遮罩背景关闭(不包括点图片本身)
  $overlay.on('click', function (e) {
    if ($(e.target).is($overlay)) {
      closeLightbox();
    }
  });

  // ---- 左右切换 ----
  $('.lightbox-prev').on('click', function (e) {
    e.stopPropagation();
    showImage(curIndex - 1);
  });

  $('.lightbox-next').on('click', function (e) {
    e.stopPropagation();
    showImage(curIndex + 1);
  });

  // ---- 键盘支持 ----
  $(document).on('keydown', function (e) {
    if (!$overlay.hasClass('active')) return;
    if (e.key === 'Escape' || e.key === 'Esc')    closeLightbox();
    if (e.key === 'ArrowLeft')                      showImage(curIndex - 1);
    if (e.key === 'ArrowRight')                     showImage(curIndex + 1);
  });

  // ---- 移动端触摸滑动 ----
  var touchStartX = 0;
  var touchStartY = 0;
  var touchDeltaX = 0;

  $overlay[0].addEventListener('touchstart', function (e) {
    var t = e.touches[0];
    touchStartX = t.clientX;
    touchStartY = t.clientY;
    touchDeltaX = 0;
  }, { passive: true });

  $overlay[0].addEventListener('touchmove', function (e) {
    var t = e.touches[0];
    touchDeltaX = t.clientX - touchStartX;
    // 图片跟随手指微动
    $img.css('transform', 'translateX(' + (touchDeltaX * 0.3) + 'px)');
  }, { passive: true });

  $overlay[0].addEventListener('touchend', function () {
    $img.css('transform', '');
    if (Math.abs(touchDeltaX) > 50) {
      if (touchDeltaX < 0) { showImage(curIndex + 1); // 左滑 → 下一张 } else { showImage(curIndex - 1); // 右滑 → 上一张 } } }, { passive: true }); }); 


评论 (48)
张小明
深有同感!我也在考虑把博客从 Hugo 迁移到纯 HTML,每次更新 Hugo 版本都提心吊胆的。
2小时前
王大力
纯 HTML 虽然简单,但文章多了管理起来还是挺麻烦的吧?有没有用什么模板方案?
5小时前
李小白
"Less is more" 这句话说得太好了。在技术圈待久了,容易陷入工具崇拜的陷阱。回归本质才是正道 👍
昨天
陈默默
达芬奇那句话太应景了哈哈。不过说实话,纯 HTML 最大的痛点是没有 Markdown 支持,写长文体验不太好。
2天前