这个挺酷:原生JS代码实现:lightbox.+图片滚动(上:lightbox部分)(含源码下载)

需求发端:

我自己动手做的一个网站,需要展示一组不明确数量和大小的图片。我想做个lightbox效果,又不想用什么库文件。网上的lightbox插件很多,功能很好很强大。但我不想要那么多代码。

于是,就自力更生自己搞一个。现在把代码分享出来,方便跟我同样需要的人。接下来是看图说话:

最终代码代码实现了一组数量未知的图片,横向排列(统一宽度和高度),容器固定宽度后,图片列表可左右滚动。图片点击,展开大图,可通过鼠标点击屏幕左右两侧箭头或按键盘方向键切换图片。点关闭返回图片列表。

这一篇是上篇,我们先实现lightbox部分的功能(代码极其简单)。【本例完整版源码下载】

废话不多说,先上一下页面布局代码:

<div id="myLightbox" class="lightbox"><!-- 遮罩层 -->
  <span class="lightbox-close">&times;</span> <!-- 关闭按钮 -->
  <span class="lightbox-before"></span>
<!-- 前一张 -->
  <span class="lightbox-after"></span>
<!-- 后一张 -->
  <div class="lightbox-content"><!-- 照片容器 -->
    <img>
    <div id="imgAlt" class="lightbox-alt"></div>
  </div>
</div>

<div class="imgbox"> <!-- 图片列表 -->
  <img src="images/0001.png" alt="纱帐香飘兰麝,娥眉轻把萧吹" onclick="openLightbox(this);">
  <img src="images/0002.png" alt="清风不识字,何故乱翻书?" onclick="openLightbox(this);">
  <img src="images/0003.png" alt="好雨知时节,当春乃发生。" onclick="openLightbox(this)">
  <img src="images/0004.png" alt="花径不曾缘客扫,蓬门今始为君开。" onclick="openLightbox(this)">
  <img src="images/0005.png" alt="锄禾日当午,汗滴禾下土" onclick="openLightbox(this)">
  <img src="images/0006.jpeg" alt="晓看天色暮看云,行也思君,坐也思君" onclick="openLightbox(this)">
  <img src="images/0007.jpeg" alt="鸳鸯被里成双夜,一树梨花压海棠。" onclick="openLightbox(this)">
  <img src="images/0008.jpeg" alt="衣带渐宽终不悔,为伊消得人憔悴。" onclick="openLightbox(this)">
  <img src="images/0009.jpeg" alt="侍儿扶起娇无力,始是新承恩泽时。" onclick="openLightbox(this)">
  <img src="images/0010.jpeg" alt="我与你生同一个衾,死同一个椁" onclick="openLightbox(this)">
<!-- Add more images here -->
</div> 

然后,给他们赋予样式表:

  .lightbox {
    display: none;
    position: fixed;
    z-index: 999;
    padding-top: 100px;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0,0.9);
  }
  .lightbox-content{    
    position: fixed;
    z-index: 9999;
    padding-top: 10px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);         
  }

  .lightbox-content img{
    max-height: 600px;
    max-width: 100%; 
    margin:auto;    
    background-color: #fff;
    border:15px solid #fff;
    border-bottom:60px solid #fff;  
  }

  .lightbox-alt{
    position: absolute;
    width: 100%;
    bottom:25px;
    text-align: center;
    font-size: 1.2rem;
    font-weight: 700;
    color: #333;
  }

  .lightbox-before,.lightbox-after{
    position: absolute;
    width: 78px;
    height: 100px;
    display: block;
    top:40%;
    filter: grayscale(70%) blur(0.5px); /* 调整数值来改变模糊程度 */
    opacity: 0.3; 
    cursor: pointer;
  }
  .lightbox-before{
    left:0px;
    background: url("images/left-arrow.png") left bottom no-repeat;
    border-radius: 0px 15px 15px 0px;
  }
  .lightbox-after{
    right:0px;
    background: url("images/right-arrow.png") left bottom no-repeat;
    border-radius:  15px 0px 0px 15px ;
  }
  .lightbox-before:hover,
  .lightbox-after:hover{
    opacity: 0.9;
    -webkit-filter: grayscale(0%); /* Chrome, Safari, Opera */
    filter: grayscale(0%);
    filter: blur(0px); /* 调整数值来改变模糊程度 */ 
    }

  .lightbox-close {
    position: absolute;
    top: 10px;
    right: 25px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
  }


  .lightbox-close:hover,
  .lightbox-close:focus {
    color: #bbb;
    text-decoration: none;
    cursor: pointer;
  }
  .imgbox{
    position: fixed;
    width: 1200px;
    height: 65px;
    overflow: hidden;    
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);   

  }
  .imgbox img{
    width: 120px;
    height: auto;
    float:left;
    cursor: pointer;
    opacity: 0.7;
    filter: grayscale(90%); /* 灰色滤镜 */
    transition: all .5s;
  }
  .imgbox img:hover{
    opacity: 1;
    filter: grayscale(0%); /* 鼠标移动到图片上,清除灰色滤镜,图片原色输出 */
    transition: all .5s;
  }

图片组准备好了,容器也都准备完成,接下来是超简洁的js时间:

  let currentImageIndex = 0;
  let allImages = document.querySelectorAll('img[onclick]');
   
  function openLightbox(imgElement) {
    // Show the lightbox 
    document.getElementById('myLightbox').style.display = "block";
    
    // Get the lightbox content and set the image
    const lightboxContent = document.querySelector('.lightbox-content img');
    lightboxContent.src = imgElement.src;
    lightboxContent.alt = imgElement.alt;
    document.getElementById('imgAlt').innerHTML = lightboxContent.alt;
    
    // Get the index of the image that was clicked
    currentImageIndex = Array.prototype.indexOf.call(allImages, imgElement);
  }
   
  // Close the lightbox
  function closeLightbox() {
    document.getElementById('myLightbox').style.display = "none";
  }

  // Show the next image
  function showNext() {
    if (currentImageIndex < allImages.length - 1) {
      openLightbox(allImages[currentImageIndex + 1]);
    }
  }
   
  // Show the previous image 
  function showPrevious() {
    if (currentImageIndex > 0) {
      openLightbox(allImages[currentImageIndex - 1]);
    }
  }
   
  // Listen for clicks on the close button 监听鼠标事件
  document.querySelector('.lightbox-close').addEventListener('click', function() {
    closeLightbox();  
  });

  document.querySelector('.lightbox-before').addEventListener('click', function() {
    showPrevious();  
  });
   
  document.querySelector('.lightbox-after').addEventListener('click', function() {
    showNext();  
  });
   
   
  // Listen for keyboard events when lightbox is open 监听键盘事件
  document.addEventListener('keydown', function(event) {
    if (document.getElementById('myLightbox').style.display === 'block') {
      if (event.key === 'ArrowLeft') {
        showPrevious();
      } else if (event.key === 'ArrowRight') {
        showNext();
      } else if (event.key === 'Escape') {
        closeLightbox();
      }
    }
  });

好了,至此,我们的lightbox部分的功能已经完全实现,效果图如下:

这个阶段,我实现了lightbox应有的简单功能。展开大图,左右切换,关闭返回。下一篇,我们将完成图片组滚动部分。希望对您有所帮助!

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Gantt 的 `lightbox.sections` 属性用于定义在任务弹窗中显示的不同部分。该属性的值是一个数组,每个元素都是一个对象,用于定义一个部分。 以下是一个示例: ```javascript gantt.config.lightbox.sections = [ {name: "description", height: 70, map_to: "text", type: "textarea", focus: true}, {name: "time", type: "duration", map_to: "auto"} ]; ``` 上面的示例中,定义了两个部分,一个是任务描述部分,另一个是时间部分。其中,`name` 属性定义了部分的名称,`height` 属性定义了部分的高度,`map_to` 属性定义了将哪个数据字段映射到当前部分,`type` 属性定义了当前部分的类型,`focus` 属性定义了是否在打开弹窗时将焦点放在当前部分中。 如果你想要保存 `lightbox.sections` 属性的值,你可以使用 `gantt.config.lightbox.sections` 属性进行保存。例如,你可以在 `onLoad` 事件中将其保存到本地存储中: ```javascript gantt.attachEvent("onLoad", function(){ var sections = gantt.config.lightbox.sections; localStorage.setItem("gantt_sections", JSON.stringify(sections)); }); ``` 在下一次加载 Gantt 图表时,你可以从本地存储中获取 `lightbox.sections` 属性的值,并将其设置为 `gantt.config.lightbox.sections` 的值: ```javascript gantt.attachEvent("onLoad", function(){ var sections = localStorage.getItem("gantt_sections"); if (sections){ gantt.config.lightbox.sections = JSON.parse(sections); } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼仰泳

码字不易,诚待支持,吾道不孤!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值