简体   繁体   English

尝试根据与按钮顺序的匹配来打开div [ordered](类似于手风琴)

[英]Trying to open div [ordered] based on match to button order (Similar to Accordion)

I'm trying to create an experience on my personal site that allows users to click on a hero image of my work and watch a div open above the work in order to see more in depth info. 我正在尝试在我的个人网站上创建一种体验,允许用户单击我的作品的英雄图片并观看作品上方的div打开,以便查看更多的深入信息。

Here's a jsFiddle: https://jsfiddle.net/1zs7zomo/ 这是一个jsFiddle: https ://jsfiddle.net/1zs7zomo/

I've tried matching the order of the info div to the order of the buttons using 我试着使用匹配匹配信息div的顺序与按钮的顺序

$(function() {
  var hero = $('.display-section__content-hero');
  var treasure = $('.treasure-container');
  var closeButton = $('.close-button');

  treasure.hide();

  hero.click(function() {
    var el = $(this);
    var elData = el.data('order');
    var treasureData = treasure.data('order');

    treasure.each(function(){
      if (treasureData === elData) {
        console.log(treasureData + " = " + elData);
        $(this).slideDown(600);
      }
    });

  })

  closeButton.click(function() {
    $(this).closest('.treasure-container').slideUp(600);
  })
})

But when it opens the info div, it opens all of them instead of the one that matches. 但是,当打开info div时,它会打开所有它们而不是匹配的一个。

I also tried matching based on .eq() position 我也尝试根据.eq()位置进行匹配

$(function() {
  var hero = $('.display-section__content-hero');
  var treasure = $('.treasure-container');
  var closeButton = $('.close-button');

  treasure.hide();

  hero.click(function() {
    var el = $(this);
    var elOrder = el.eq();

    treasure.eq(elOrder).slideDown(600);
  })

  closeButton.click(function() {
    $(this).closest('.treasure-container').slideUp(600);
  })
})

But that doesn't seem to work at all. 但这似乎根本不起作用。 I supposed I could get into putting specific matching classes on each, but I'm trying to make this as modular as possible. 我以为可以在每个类上放置特定的匹配类,但是我正在尝试使其尽可能模块化。 I'm building on Node.js right now to learn and want to make this super modular so that down the road when I figure out how to load data via ajax I can just have them react without having to use any form of order or class, purely data. 我现在正在Node.js上进行构建,以学习并希望使它成为超级模块化,这样一来,当我弄清楚如何通过ajax加载数据时,我就可以让它们做出反应而不必使用任何形式的顺序或类,纯粹是数据。

Here's the HTML at a base level.. can provide more if necessary 这是基本级别的HTML。如果需要,可以提供更多内容

<div class="display-section--build clearfix">
  <div class="treasure-container" data-order="1">
    <div class="treasure-hero">
      <div class="close-button">X</div>
    </div>
    <div class="treasure-description-container">
      <div class="description-head">
        <h1 class="treasure-description__title">Spiffly.is</h1>
        <a class="treasure-description__link" href="http://spiffly.is" target="_blank">Visit Site</a>
      </div>
      <h2 class="treasure-description__role">Front-End Engineer</h2>
      <p class="treasure-description__details">Spiffly is a marketplace for good. A place for B2P -- that's business to prosumer -- transactions. Prosumers get products at wholesale cost, which takes the risk out of trying something new and gives them motivation to promote the product if it's good. Businesses still make money but get marketing from prosumers' social presence. 
      <br>
      <br>
      I built the front-end of Spiffly.is, an e-commerce site, in less than 100 hours despite never having worked with Swig, Node.js or Sass before.
      </p>
    </div>
  </div>
  <div class="display-section__content-hero" data-order="1"></div>
</div>
treasure.each(function(index, obj){
    if ($(obj).data('order') === elData) {

Maybe that snippet will help you. 也许该片段将对您有所帮助。

You should use a data attribute to index the box you need to target 您应该使用数据属性来索引需要定位的框

<div class="click_me_to_open" data-order="1"></div>
<div class="hidden_div" data-order-details="1"></div>

this way in your JS you bind the click to .click_me_to_open get the value of data-order and make a selector to target the right box 这样,在您的JS中,您可以将click绑定到.click_me_to_open获取data-order的值,并通过选择器定位到正确的框

$('.click_me_to_open').on('click', function () {
      var target = $(this).data('order');

      $('[data-order-details="' + target + '"]').slideDown();
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM