【问题标题】:trigger animations independently for elements with the same class Javascript using greensock使用greensock为具有相同类Javascript的元素独立触发动画
【发布时间】:2019-10-27 05:21:20
【问题描述】:

我在产品系列页面上有多个产品,我想在每次单独悬停产品卡片时触发动画。

问题:当我将每个产品卡悬停时,动画会同时触发所有产品卡。
我在这里查看了其他类似的问题,但找不到针对我的具体问题的解决方案。

var main = document.querySelectorAll(".main-card");
var anime = document.querySelectorAll(".bottom-card");
        
        if (main.length !== 0){
  for (var i=0; i<main.length; i++) {
(this).main[i].addEventListener("mouseenter", function(){

  TweenMax.to(anime, 0.3, {opacity: 1, height: "143px"});
//   .staggerFrom(".size", 0.5, {y: 10, opacity:0, ease:Elastic.easeOut}, 0.1)
  });

(this).main[i].addEventListener("mouseleave", function(){
  TweenMax.to(anime, 0.3, {opacity: 0, height: '0px'});
  });
    
  };
          
        
        }
<form  action="/cart/add" method="post" id="AddToCartForm">
  <a class="grid-view-item__link grid-view-item__image-container" href="{{ product.url | within: collection }}">
  <div id="card" class="product-card__image-with-placeholder-wrapper" data-image-with-placeholder-wrapper>
    <div id="{{ wrapper_id }}" class="grid-view-item__image-wrapper product-card__image-wrapper js">
      <div style="padding-top:{% unless product.featured_image == blank %}{{ 1 | divided_by: product.featured_image.aspect_ratio | times: 100 }}%{% else %}100%{% endunless %};">
        <div id="id" class="main-card">
          <div class="image-card">
        <img id="{{ img_id }}"
              class="grid-view-item__image lazyload"
              alt="{{ product.featured_image.alt }}"
              data-src="{{ img_url }}"
              data-widths="[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]"
              data-aspectratio="{{ product.featured_image.aspect_ratio }}"
              data-sizes="auto"
              data-image>
        
            </a>
         
          
          <a class="grid-view-item__link grid-view-item__image-container" href="{{ product.url | within: collection }}">
          
          <div class="h4 grid-view-item__title product-card__title" aria-hidden="true">{{ product.title }}</div>
  
  {% include 'product-price', variant: product.selected_or_first_available_variant %}
          </a>
          
          <div id="bottom" class="bottom-card">
            
            
  
  {% if product.title == "Blue Silk Tuxedo" %}

            
 
    
  <div class="flex">
<select name="id" id="{{ product.handle }}" style="display: none;">

  
{% for variant in product.variants %}
  {% if variant.available == true %}
<option {% unless variant.available %} disabled="disabled" {% endunless %} {% if variant == current_variant %} selected="selected" {% endif %} data-sku="{{ variant.sku }}" value="{{ variant.id }}">{{ variant.title }} - {% if variant.available %}{{ variant.price | money_with_currency }}{% else %}{{ 'products.product.sold_out' | t }}{% endif %}</option> 
  

  
{% else %}
  <option disabled="disabled">{{ variant.title }} - Sold Out</option> 

  {% endif %}
  {% endfor %} 
</select> 
    {% if product.available and product.variants.size > 1 %}
  {% for option in product.options %}
    {% include 'swatch' with option %}
  {% endfor %}
{% endif %}
  </div>
  
  
 <button type="submit" name="add" id="AddToCart" class="btn">
<span id="AddToCartText">Add to cart</span>
  
  
</form>

每个产品都有这个 html 代码。所以每个产品都有相同的类。这是一个 shopify 主题。

【问题讨论】:

    标签: javascript html shopify gsap


    【解决方案1】:

    您正在使用下面的 2 行来定位类 .bottom-card 的所有元素。

    // This returns all the elements with class .bottom-card in the document
    var anime = document.querySelectorAll(".bottom-card");
    
    // Here you are animating all the elements
    TweenMax.to(anime, 0.3, {opacity: 1, height: "143px"})
    

    您可以在 .main-card div 上运行 querySelector 以仅获取其类为 .bottom-card 的子级。

    TweenMax.to(main[index].querySelector(".bottom-card"), 0.3, { opacity: 1, height: "143px" })
    

    试试这个打击 SO sn-p 看看动画效果。

    var main = document.querySelectorAll(".main-card");
    
    if (main.length !== 0) {
      for (var i = 0; i < main.length; i++) { 
        // Closure for each function
        let index = i; 
        main.item(i).addEventListener("mouseenter", function() {          
        TweenMax.to(main[index].querySelector(".bottom-card"), 0.3, { opacity: 1, height: "143px" });
        });
    
        main[i].addEventListener("mouseleave", function() {
          TweenMax.to(main[index].querySelector(".bottom-card"), 0.3, { opacity: 0, height: "0px" });
        });
      }
    }
    html, body {
      height: 100%;
    }
    
    body {
      background-color:#1d1d1d;
      font-family: "Asap", sans-serif;
      color:#989898;
      margin:0 10px;
      font-size:16px;
    }
    
    h1, h2, h3 {
      font-family: "Signika Negative", sans-serif;
      margin: 10px 0 10px 0;
      color:#f3f2ef;
    }
    
    h1 { 
      font-size:36px;
    }
    
    h2 {
      font-size:30px;
    }
    
    h3 {
      font-size:24px;
    }
    
    p{
      line-height:22px;
      margin-bottom:16px;
      width:650px;
    }
    
    #demo {
      height:100%;
      position:relative;
    }
    .main-card {
      width:50px;
      min-height:50px;
      position:relative;
      border-radius:6px;
      margin-top:4px;
      display:inline-block
    }
    
    .bottom-card {
      color: yellow;
    }
    .green{
      background-color:#6fb936;
    }
    
    .orange {
      background-color:#f38630;
    }
    .grey {
      background-color:#989898;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.2/TweenMax.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    
    
    <link href='https://fonts.googleapis.com/css?family=Asap:400,700' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Signika' rel='stylesheet' type='text/css'>
     <div>
        <div class="main-card green">
            <div class="bottom-card"></div>
        </div>
        <div class="main-card grey">
            <div class="bottom-card"></div>
        </div>
        <div class="main-card orange">
            <div class="bottom-card"></div>
        </div>
        <div class="main-card green">
            <div class="bottom-card"></div>
        </div>
        <div class="main-card grey">
            <div class="bottom-card"></div>
        </div>
        <div class="main-card orange">
            <div class="bottom-card"></div>
        </div>
        <div class="main-card green">
            <div class="bottom-card"></div>
        </div>
        <div class="main-card grey">
            <div class="bottom-card"></div>
        </div>
        <div class="main-card orange">
            <div class="bottom-card"></div>
        </div>
      </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多