如何在内联库中制作放大弹出窗口中的选定项?

时间:2015-07-03 03:38:46

标签: javascript jquery magnific-popup

我正在使用magnific-popup来显示内嵌库(带有自定义HTML标记),就像示例here一样。这将打开一个自定义标记库,您可以在其中单击3组头像/名称/位置数据。 (试试下面的例子或代码,你会看到我的意思)。

但是,我找不到在锚点击上打开第二个元素(或除第一个元素之外的任何元素)的方法。

有没有人使用magnific-popup而且能够打开第一个元素以外的内联元素?

HTML:

<button style="padding:20px;">Open popup</button>

CSS:

.white-popup {
  position: relative;
  background: #FFF;
  padding: 20px;
  width: auto;
  max-width: 200px;
  margin: 20px auto;
  text-align: center;
}

使用Javascript:

// Define data for the popup
var data = [
  {
    username: "Brad Frost", // Key "username" means that Magnific Popup will look for an element with class "mfp-username" in markup and will replace its inner HTML with the value.
    userWebsite_href: 'http://www.bradfrostweb.com', // Key "userWebsite_href" means that Magnific Popup will look for an element with class "mfp-userWebsite" and will change its "href" attribute. Instead of ending "href" you may put any other attribute.
    userAvatarUrl_img: 'https://si0.twimg.com/profile_images/1561258552/brad_frost_bigger.png', // Prefix "_img" is special. With it Magnific Popup finds an  element "userAvatarUrl" and replaces it completely with image tag.
    userLocation: 'Pittsburgh, PA'
  },
  {
    username: "Paul Irish",
    userWebsite_href: 'http://paulirish.com',
    userAvatarUrl_img: 'https://si0.twimg.com/profile_images/2910976341/7d972c32f3882f715ff84a67685e6acf_bigger.jpeg',
    userLocation: 'San Francisco'

  },

  {
    username: "Chris Coyier",
    userWebsite_href: 'http://css-tricks.com',
    userAvatarUrl_img: 'https://si0.twimg.com/profile_images/1668225011/Gravatar2_bigger.png',
    userLocation: 'Palo Alto, California'
  }
];

// initalize popup
$('button').magnificPopup({ 
  key: 'my-popup', 
  items: data,
  type: 'inline',
  inline: {
    // Define markup. Class names should match key names.
    markup: '<div class="white-popup"><div class="mfp-close"></div>'+
              '<a class="mfp-userWebsite">'+
                '<div class="mfp-userAvatarUrl"></div>'+
                '<h2 class="mfp-username"></h2>'+
              '</a>'+
              '<div class="mfp-userLocation"></div>'+
            '</div>'
  },
  gallery: {
    enabled: true 
  },
  callbacks: {
    markupParse: function(template, values, item) {
      // optionally apply your own logic - modify "template" element based on data in "values"
      // console.log('Parsing:', template, values, item);
    }
  }
});

3 个答案:

答案 0 :(得分:1)

将此添加到您的magnificPopup功能,这将在点击时打开第二个项目:

index:2,

在文档here上列出。

Example Codepen

答案 1 :(得分:1)

查看此Codepen: http://codepen.io/mgo/pen/ykgjb

或者尝试解决问题的方法:https://codepen.io/jnax/pen/RpyVxx

var clickedButton;
   $('button').click(function(){
   clickedButton = $('button').index(this);
   }); //this indexing solution is obviously not the best for all situations

然后,当弹出窗口打开时,转到弹出窗口中的相应索引。

callbacks: {
  open: function(){$('button').magnificPopup('goTo',clickedButton)}}

问题是,goTo动作是可见的,虽然我认为它可以被css过渡掩盖

答案 2 :(得分:0)

上述内容的细微变化对我有用


// Define data for the popup
var data = [
  {
    username: "Brad Frost", // Key "username" means that Magnific Popup will look for an element with class "mfp-username" in markup and will replace its inner HTML with the value.
    
    userWebsite_href: 'http://www.bradfrostweb.com', // Key "userWebsite_href" means that Magnific Popup will look for an element with class "mfp-userWebsite" and will change its "href" attribute. Instead of ending "href" you may put any other attribute.
    
    userAvatarUrl_img: 'https://si0.twimg.com/profile_images/1561258552/brad_frost_bigger.png', // Prefix "_img" is special. With it Magnific Popup finds an  element "userAvatarUrl" and replaces it completely with image tag.
    
    userLocation: 'Pittsburgh, PA'
  },
  
  {
    username: "Paul Irish",
    userWebsite_href: 'http://paulirish.com',
    userAvatarUrl_img: 'https://si0.twimg.com/profile_images/2910976341/7d972c32f3882f715ff84a67685e6acf_bigger.jpeg',
    userLocation: 'San Francisco'

  },
  
  {
    username: "Chris Coyier",
    userWebsite_href: 'https://css-tricks.com',
    userAvatarUrl_img: 'https://si0.twimg.com/profile_images/1668225011/Gravatar2_bigger.png',
    userLocation: 'Palo Alto, California'
  }
];

// initalize popup
 $('button').click(function(){
  let index = $('button').index(this);
   console.log("button index",index)
  $(this).magnificPopup({ 
  key: 'my-popup', 
  items: data,
  type: 'inline',
 index:index,
  inline: {
    // Define markup. Class names should match key names.
    markup: '<div class="white-popup"><div class="mfp-close"></div>'+
              '<a class="mfp-userWebsite">'+
                '<div class="mfp-userAvatarUrl"></div>'+
                '<h2 class="mfp-username"></h2>'+
              '</a>'+
              '<div class="mfp-userLocation"></div>'+
            '</div>'
  },
  gallery: {
    enabled: true 
  },
  callbacks: {
    markupParse: function(template, values, item) {
      // optionally apply your own logic - modify "template" element based on data in "values"
      // console.log('Parsing:', template, values, item);
    },
    open: function() {
      // Will fire when this exact popup is opened
      // this - is Magnific Popup object
      console.log("inside open",index)
    },
  }
});



   });