jQuery实现查看图片功能

 更新时间:2020年12月01日 09:22:56   作者:从入门to入万  
这篇文章主要为大家详细介绍了jQuery实现查看图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了jQuery实现查看图片的具体代码,供大家参考,具体内容如下

HTML

<!-- 放缩略图的div -->
  <div class="sl">
    <img src="images/1.jpg" />
    <img src="images/2.jpg" />
    <img src="images/3.jpg" />
  </div>
  <!-- 实现关灯的div -->
  <div class="gd"></div>
  <div class="yt">
    <!-- 左翻按钮 -->
    <div class="left">
      <img src="images/left.png" alt="" width="100">
    </div>
    <div class="tp">
      <!-- 展示原图 -->
      <img src="images/1.jpg" class="show" />
      <!--放开始和结束图片的盒子 -->
       <div class="ss" style="display: none;">
        <img src="images/start.png" alt="" width="50" class="start">
      </div>
    </div>
    <!-- 右翻按钮 -->
    <div class="right">
      <img src="images/right.png" alt="" width="100">
    </div>
</div>

CSS

 html,body{
    padding: 0;
    margin: 0;
  }
  .sl img {
    width: 300px;
  }

  .gd {
    background-color: rgba(0, 0, 0, 0.7);
    position: absolute;
    z-index: 900;
    display: none;
    top: 0;
    left: 0;
  }

  .sl {
    position: absolute;
    z-index: 888;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
  }

  .sl>img {
    width: 25%;
  }

  .yt {
    z-index: 990;
    position: absolute;
    display: none;
    left: 500px;
    top: 200px;
  }

  .tp {
    margin: 0 20px;
  }

  .yt>div {
    display: inline-block;
  }

  .left,
  .right {
    position: relative;
    top: -110px;
    cursor: pointer;
  }

  .ss {
    position: relative;
    width: 50px;
    height: 50px;
    left: 270px;
  }

  .start {
    z-index: 990;
    position: absolute;
  }

JS

var max = $(".sl img").length;
$(function (e) {
  var width = $(window).width();
  var height = $(window).height();
  $(".gd").css({
    "height": height,
    "width": width
  });

  //左翻按钮动画
  $(".left").hover(
    function () {
      $(this).animate({
        "left": "-10"
      });
    },
    function () {
      $(this).animate({
        "left": "0"
      });
    }
  );
  //右翻按钮动画
  $(".right").hover(
    function () {
      $(this).animate({
        "right": "-10"
      });
    },
    function () {
      $(this).animate({
        "right": "0"
      });
    }
  );

  //被点击的缩略图
  $(".sl>img").click(function (e) {
    $(".gd").show(500);
    $(".yt").fadeIn(800);
    var index = $(this).index(); //当前被点击图片的索引
    $(".tp>img").attr("src", "images/" + (index + 1) + ".jpg");
    //左翻
    $(".left").click(function (e) {
      if (index - 1 < 0) {
        index = max - 1;
      } else {
        index = index - 1;
      }
      $(".tp>img").attr("src", "images/" + (index + 1) + ".jpg");
    });
    //右翻
    $(".right").click(function (e) {
      if (index == max - 1) {
        index = 0;
      } else {
        index = index + 1;
      }
      $(".tp>img").attr("src", "images/" + (index + 1) + ".jpg");
    });

    //隐藏和显示播放按钮
    $(".tp").hover(
      function () {
        $(".ss").fadeIn(500);
        $(this).animate({
          "opacity": "0.7"
        }, 700);
      },
      function () {
        $(".ss").fadeOut(500);
        $(this).animate({
          "opacity": "1"
        }, 700);
      }
    );
 //点击开始播放 再次点击结束播放
    let flag = true;
    $(".start").click(function () {
      if (flag == true) {
        time = setInterval(function () {
          if (index == max - 1) {
            index = 0;
          } else {
            index = index + 1;
          }
          $(".tp>img").attr("src", "images/" + (index + 1) + ".jpg");
        }, 2000);
        flag = false;
        $(".start").attr("src", "images/stop.png");
      } else {
        clearInterval(time);
        flag = true;
        $(".start").attr("src", "images/start.png");
      }
    });
    let h = $(".tp>img").height();
    $(".ss").css({
      "top": -h / 2 - 25
    });
    //隐藏关灯效果
    $(".gd").click(function () {
      $(".gd").hide(800);
      $(".yt").fadeOut(500);
    });
  });
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 使用jQuery实现的掷色子游戏动画效果

    使用jQuery实现的掷色子游戏动画效果

    大家一定都玩过掷色子的游戏,今天我给大家分享的是如何使用jQuery来实现掷色子的动画效果,通过jQuery的animate()自定义动画函数并结合CSS背景图片切换实现的动画效果
    2014-03-03
  • jquery之empty()与remove()区别说明

    jquery之empty()与remove()区别说明

    要用到移除指定元素的时候,发现empty()与remove([expr])都可以用来实现。可仔细观察效果的话就可以发现。
    2010-09-09
  • jQuery中parents()方法用法实例

    jQuery中parents()方法用法实例

    这篇文章主要介绍了jQuery中parents()方法用法,实例分析了parents()方法的功能、定义及取得一个包含着所有匹配元素的父辈元素的元素集合使用技巧,需要的朋友可以参考下
    2015-01-01
  • Jqgrid之强大的表格插件应用

    Jqgrid之强大的表格插件应用

    jqGrid是一款基于jQuery的功能强大的表格插件,使用jqGrid可以轻松实现前端页面与后台数据进行ajax异步通信,jqGrid运行速度相当快,可以很好的应用在一些后台管理系统来管理大量数据的场合
    2015-12-12
  • jquery实现简易的移动端验证表单

    jquery实现简易的移动端验证表单

    本文给大家汇总介绍了几个常用的jquery实现简易的移动端验证表单,非常的实用,有需要的小伙伴可以进来参考下。
    2015-11-11
  • jQuery实现文章收起与展开功能

    jQuery实现文章收起与展开功能

    这篇文章主要为大家详细介绍了jQuery实现文章收起与展开功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • jquery 设置元素相对于另一个元素的top值(实例代码)

    jquery 设置元素相对于另一个元素的top值(实例代码)

    在jquery中offset().top是相对于body来说的,另外在设置top值的时候要找到与该元素最近的有相对值的元素
    2013-11-11
  • jquery.simple.tree插件 更简单,兼容性更好的无限树插件

    jquery.simple.tree插件 更简单,兼容性更好的无限树插件

    在这里介绍一款小巧,功能强大,能拖拽,支持异步,且兼容性更高的jquery Tree插件
    2010-09-09
  • jquery获取颜色在ie和ff下的区别示例介绍

    jquery获取颜色在ie和ff下的区别示例介绍

    在使用$(#id).attr(color) 获取颜色的时候,ie和ff是不同的,下面有个示例,大家可以参考下
    2014-03-03
  • 常见的jQuery选择器汇总

    常见的jQuery选择器汇总

    本文汇总介绍了常见的jQuery选择器知识,包含基本元素选择器、分层选择器、基本条件选择器、内容条件选择器、可见性条件选择器、属性选择器、子元素选择器、表单元素选择器、表单属性选择器。十分的详尽,有需要的小伙伴参考下吧
    2014-11-11

最新评论