gpt4 book ai didi

javascript - 使用 greensock 为具有相同类 Javascript 的元素独立触发动画

转载 作者:行者123 更新时间:2023-11-30 19:10:07 24 4
gpt4 key购买 nike

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

问题:当我悬停每个产品卡片时,所有产品卡片的动画同时触发。
我在这里查看了其他类似问题,但找不到解决我的特定问题的方法。

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 主题。

最佳答案

您使用下面的 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" })

尝试 blow SO 片段以查看效果动画。

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>

关于javascript - 使用 greensock 为具有相同类 Javascript 的元素独立触发动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58576832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com