- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在产品系列页面上有多个产品,我想在每次分别悬停产品卡片时触发动画。
问题:当我悬停每个产品卡片时,所有产品卡片的动画同时触发。
我在这里查看了其他类似问题,但找不到解决我的特定问题的方法。
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/
我想知道如何在 GreenSock JavaScript 库中制作无限动画。我知道JavaScript当时只能进行1次操作,但我希望一些SVG图像能够不停地移动。对我来说,它看起来像无限循环,但我知道
我正在尝试对齐所有子补间的开始,希望它们立即开始,没有任何延迟,但似乎是按顺序完成的,一个 child 完成,然后第二个开始,第三个在第二个之后开始等等...不知道我在这里做错了什么? var tim
我正在尝试按以下顺序使用介绍文本和箭头制作介绍页面的动画: 背景图像来回移动两次。之后,箭头应从 1 缩放到 1.5,并再次重新缩放到原始大小 介绍文本也应缩放至 1.5 并重新缩放至其原始大小 由于
有一个 speed test在 greensock 网站上与其他动画库、JQuery 甚至 CSS Transitions 比较速度。它通过为数百/数千个分体设置动画来对 FPS 进行基准测试。 gr
我有以下代码: var mainTimeLine = new TimelineLite({paused:true}); var whiteGlow = "0px 0px 30px rgba(255,2
我想在 HTML 元素上一一添加类。 TweenMax.staggerTo(".some-class-name", 0.5, { className: "+=animation-class" }, 0
我以前从未使用过GreenSock。背景图像变化很好,它会切换和缩放,但是我面临的问题如下: Sprite 上显示的第一个背景图像不会缩放,但其他所有图像都会缩放,我该如何解决此问题? 它似乎会改变背
我正在尝试使用 Greensock 文本插件。我可以更改文本的颜色,但无法更改文本。我正在使用 jQuery 3.2.1 和 Greensock TweenMax 1.20.3 我的代码会将文本颜色更
我正在尝试使搜索栏具有动画效果。我能够在您单击时使其绘制,当您再次单击时它会返回到原始位置(使用 greensock - drawSVG)。 我希望能够在不刷新页面的情况下再次点击,并且一切正常。到目
我在鼠标事件上调用了两个函数: function menuBtnOver(e){ var b = e.data; b.setPosition(b.x, b.y+5); } functi
我正在尝试使用 JavaScript 和 Greensock 创建一个单击时滑入和滑出的导航栏。由于某种原因,当以不同尺寸单击时,单击操作随机不起作用,但有时它工作得很好。 我的代码如下,您可以在以下
我有一个正在使用 Greensock 的动画。当该动画完成并且用户单击按钮时,动画将反向播放。我遇到的问题是我希望动画反向播放的速度比最初播放时快 3 倍。我对如何做到这一点感到困惑。 我当前的代码工
我一直在使用javascript制作动画,但出现了我似乎无法解决的错误。 Link to Codepen Uncaught SyntaxError: Unexpected token '.' at
所以我正在尝试使用 Greensock 库为雪佛龙制作动画。我的目标是让 进入: 我在 Codepen https://www.codepen.io/Brushel/pen/boryZP 上有一个工作
我正在使用 GreenSock( https://greensock.com/get-started-js ) 进行动画。 问题是,当我向下滚动鼠标滚轮时,动画将从右向左工作。这不是一种权利。我需要当
我想将先前定义的 x/y 坐标数组传递给 greensock 贝塞尔曲线插件,如下所示: var ball = document.querySelector('#ball') var startCir
我正在尝试按照scrollmagic示例实现视差滚动。 greensock 文档说,像 TimelineMax.to 这样的东西定义了该场景中元素的最终位置(而不是定义初始位置的 .from)。 但是
我一直在尝试让动画在点击时触发,我已经通过 TimelineMax 创建了动画。我不明白为什么,但它一直触发这个动画的第一个实例,有 5 个 .blackout-panel 并且它为第一个 .blac
我正在尝试找到一种方法,将任意数量的元素添加到时间轴,然后在添加它们之后,同时开始运行它们。我尝试在添加到时间线之前调用 .pause() 并在之后播放,但它总是在我调用播放之前开始。 我目前正在运行
我正在嵌套时间线。 代码如下: timeLine.to(obj1,1.2,{css:{display:"block"}}) nestedTimeline1 = new TimelineMax({re
我是一名优秀的程序员,十分优秀!