• 灯箱效果插件Magnific Popup详解


    Magnific Popup 是一个非常优秀的弹出对话框或者灯箱效果插件。它基于jQuery(zepto)开发,使用非常简单,特点就是:非常好用。

    官网地址: http://dimsemenov.com/plugins/magnific-popup/

    Github地址:https://github.com/dimsemenov/Magnific-Popup

    先看个效果吧:

     

    快速入门demo

    先做一个简单的,把一个div弹出来的效果。

    第一步: 添加脚本支持

    <!-- Magnific Popup core CSS file -->
    <link rel="stylesheet" href="magnific-popup/magnific-popup.css">
    
    <!-- jQuery 1.7.2+ or Zepto.js 1.0+ -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
    <!-- Magnific Popup core JS file -->
    <script src="magnific-popup/jquery.magnific-popup.js"></script>
    

    第二步: 添加html标签

    <!-- 添加一个a标签,设置href属性指向一个要弹出来的一个div盒子。 -->
    <a href="#pop" class="button is-large has-text-primary" id="btn">添加</a>
    
    <!-- 以下就是要弹出来的层,注意:添加mfp-hide先进行隐藏起来。 -->
    <div id="pop" class="mfp-hide">
      <lable for="sname">用户名</lable><input type="text" id="sname" name="ss" value="">
      <hr>
      <input type="button" value="关闭" id="btnClose">
    </div>
    

    第三步: 给弹出来的层添加点样式

    #pop {
      position: relative;
      background: #FFF;
      padding: 20px;
       auto;
      max- 500px;
      margin: 20px auto;
    }
    

    第四步: 初始化弹出来的层和a标签的点击事件。

    // 给超级连接添加magnificPopup初始化方法。
    $('#btn').magnificPopup({
      type: 'inline',         // 行内的类型,类比图片的模式
      closeBtnInside: true,   // 显示关闭按钮
      closeOnBgClick: false  // 点击遮罩透明背景关闭弹出层
    });
    

    magnificPopup方法的选项设置

    • mainClass: String类型,要添加到根元素上的样式类。默认是空字符串。
    • closeOnContentClick: Boolean类型,默认false,点击内容区域关闭弹出层。
    • closeOnBgClick: Boolean类型,默认true,点击背景区域关闭弹出层。
    • closeBtnInside: Boolean类型,默认true,是否有内置的关闭按钮。
    • modal: Boolean类型,默认false,是否是模态对话框。

    常用的其他api

    • 关闭图层close方法: $.fn.magnificPopup('close');
    • 打开弹出层open方法:$.fn.magnificPopup('open')
    • 下一个图片next方法: $('.element-with-popup').magnificPopup('next');

    其他方法:$.fn.magnificPopup('methodName' /*, param1, param2 ... */)

    其他demo

    • 图片demo
    <div class="parent-container">
      <a href="path-to-image-1.jpg">Open popup 1</a>
      <a href="path-to-image-2.jpg">Open popup 2</a>
      <a href="path-to-image-3.jpg">Open popup 3</a>
    </div>
    
    <script>
    $('.parent-container').magnificPopup({
      delegate: 'a', // child items selector, by clicking on it popup will open
      type: 'image'
    // other options
    });
    </script>
    
    • 图片demo2
    <div class="html-code grid-of-images">
          <div class="popup-gallery">
            <a href="http://farm9.staticflickr.com/8242/8558295633_f34a55c1c6_b.jpg" title="The Cleaner"><img src="http://farm9.staticflickr.com/8242/8558295633_f34a55c1c6_s.jpg" width="75" height="75" /></a>
            <a href="http://farm9.staticflickr.com/8382/8558295631_0f56c1284f_b.jpg" title="Winter Dance"><img src="http://farm9.staticflickr.com/8382/8558295631_0f56c1284f_s.jpg" width="75" height="75" /></a>
            <a href="http://farm9.staticflickr.com/8225/8558295635_b1c5ce2794_b.jpg" title="The Uninvited Guest"><img src="http://farm9.staticflickr.com/8225/8558295635_b1c5ce2794_s.jpg" width="75" height="75" /></a>
            <a href="http://farm9.staticflickr.com/8383/8563475581_df05e9906d_b.jpg" title="Oh no, not again!"><img src="http://farm9.staticflickr.com/8383/8563475581_df05e9906d_s.jpg" width="75" height="75" /></a>
            <a href="http://farm9.staticflickr.com/8235/8559402846_8b7f82e05d_b.jpg" title="Swan Lake"><img src="http://farm9.staticflickr.com/8235/8559402846_8b7f82e05d_s.jpg" width="75" height="75" /></a>
            <a href="http://farm9.staticflickr.com/8235/8558295467_e89e95e05a_b.jpg" title="The Shake"><img src="http://farm9.staticflickr.com/8235/8558295467_e89e95e05a_s.jpg" width="75" height="75" /></a>
            <a href="http://farm9.staticflickr.com/8378/8559402848_9fcd90d20b_b.jpg" title="Who's that, mommy?"><img src="http://farm9.staticflickr.com/8378/8559402848_9fcd90d20b_s.jpg" width="75" height="75" /></a>
          </div>
        </div>
        <script type="text/javascript">
          $(document).ready(function() {
            $('.popup-gallery').magnificPopup({
              delegate: 'a',
              type: 'image',
              tLoading: 'Loading image #%curr%...',
              mainClass: 'mfp-img-mobile',
              gallery: {
                enabled: true,
                navigateByImgClick: true,
                preload: [0,1] // Will preload 0 - before current, and 1 after the current image
              },
              image: {
                tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
                titleSrc: function(item) {
                  return item.el.attr('title') + '<small>by Marsel Van Oosten</small>';
                }
              }
            });
          });
        </script>
      </div>
    

    总结

    这个插件还是非常好用的,使用简单方便,而且api也都很人性化。压缩后的js才20k,也算很小,lightbox效果也很棒。你值得拥有。

  • 相关阅读:
    HMM 学习一
    matlab buffer的使用
    JDK环境变量配置及Tomcat安装服务
    Js参数RSA加密传输,jsencrypt.js的使用
    Dapper基本增删改查
    Dapper事务操作
    Ubuntu知识记录
    InstallShield 覆盖安装
    Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
    Sql Server导出表结构Excel
  • 原文地址:https://www.cnblogs.com/fly_dragon/p/9146157.html
Copyright © 2020-2023  润新知