【问题标题】:Script JQuery Gallery, pass the CSS class as parameter of a function脚本 JQuery Gallery,将 CSS 类作为函数的参数传递
【发布时间】:2016-12-09 00:43:27
【问题描述】:

您好,我是 javascript/编程新手,我无法为负责为每个照片库提供参数的脚本找到合理的解决方案。该脚本运行良好,但正如我所写,我认为它可以简化为不重复参数。每个图库脚本中只有一个参数(类选择器)发生变化。

$(document).ready(function() {
      $('.popup-gallery-wakipo').magnificPopup({ // HERE THE CLASS SELECTOR
        delegate: 'a.gallery_portf',
        type: 'image',
        tLoading: 'Loading image #%curr%...',
        mainClass: 'mfp-img-mobile',
        gallery: {
          enabled: true,
          navigateByImgClick: true,
          preload: [0,1]
        },
        image: {
          tError: '',
          titleSrc: function(item) {
            return item.el.attr('title') + '';
            }
        }
      });


      $('.popup-gallery-instacash').magnificPopup({ // HERE THE CLASS SELECTOR
        delegate: 'a.gallery_portf',
        type: 'image',
        tLoading: 'Loading image #%curr%...',
        mainClass: 'mfp-img-mobile',
        gallery: {
          enabled: true,
          navigateByImgClick: true,
          preload: [0,1]
        },
        image: {
          tError: '',
          titleSrc: function(item) {
            return item.el.attr('title') + '';
          }
        }
      });

等等……

所以问题是如何模块化这个脚本,只将类选择器作为参数传递。也许使用对象构造器/方法,我已经尝试过,但我迷失了让它工作

  function Gallery (theclass){

    $('.' + theclass).magnificPopup({
      delegate: 'a.gallery_portf',
      type: 'image',
      tLoading: 'Loading image #%curr%...',
      mainClass: 'mfp-img-mobile',
      gallery: {
        enabled: true,
        navigateByImgClick: true,
        preload: [0,1]
      },
      image: {
        tError: '',
        titleSrc: function(item) {
          return item.el.attr('title') + '';

        }
      }
    });

  }

  var mySecondGallery = new Gallery("popup-gallery-wakipo");

  mySecondGallery ();

【问题讨论】:

    标签: javascript jquery parameters gallery image-gallery


    【解决方案1】:

    试试这个:

    function gallery(class) { 
      return $("." + class).magnificentPopup({
      ...
      });
    }
    var mySecondGallery = gallery("popup-gallery-wakipo");
    

    然后您就可以访问 jQuery 对象了。

    【讨论】:

    • 对不起,我是这样使用的,但无法正常工作。这里是原始JS文件:pastebin.com/tM002P5U
    • 您需要访问 jQuery 对象,还是只执行代码就足够了?
    • 我不确定哪个 JS 库正在使用画廊 :-/,我只知道这个脚本可以让它工作。你在 las 行中写的那个任务不需要以某种方式调用吗?这里是其中一个画廊的 HTML:pastebin.com/bqWATSpv 找出第 4 行中的类
    • 你为什么不给所有的元素同一个类并且只执行一次maginficentPopup-code?​​span>
    • 因为是画廊画廊,所以每个画廊都有自己的班级
    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多