[英]How to change HTML AOS delay value using javascript or CSS
First time seeking help on here, hoping there is a simple answer for this.第一次在这里寻求帮助,希望有一个简单的答案。 I am using AOS - Animate on Scroll library in my website and I want to be able to change the aos delay value based on the screen width as I need different delay times depending on screen size.
我在我的网站上使用 AOS - 滚动库上的动画,我希望能够根据屏幕宽度更改 aos 延迟值,因为我需要根据屏幕大小不同的延迟时间。
Is it possible to call the below “1300” value something like “number” and then have that value changed by media queries in either CSS or javascript?是否可以将下面的“1300”值称为“数字”之类的值,然后通过 CSS 或 javascript 中的媒体查询更改该值?
<div class="skill" data-aos="fade-in" data-aos-delay="1300">
<div class="icon-container">
<img src="images/pencilruler.gif" alt="">
</div>
<h1>Graphic Design</h1>
<p>
Custom designs for screen and print. From logo designs and corporate branding, to adverts and packaging.
</p>
</div>
I have have had no luck finding a solution to this.我没有运气找到解决方案。 I tried using getElementById() in javascript but could only manage to change the content of the div and not a value within it.
我尝试在 javascript 中使用 getElementById() 但只能设法更改 div 的内容而不是其中的值。
At the moment my workaround (below) is to have duplicate divs for each delay length and then by using 'display:none;'目前,我的解决方法(如下)是为每个延迟长度设置重复的 div,然后使用“display:none;” I can remove the unneeded ones based on screen size.
我可以根据屏幕大小删除不需要的。 This is a real pain and a bit messy as there is quite a few delay variations that I want.
这是一个真正的痛苦和有点混乱,因为有很多我想要的延迟变化。
@media screen and (max-width : 419px){
.skill-desktop{
display:none;
}
}
Appreciate any help or ideas.感谢任何帮助或想法。
Like Hugo Elhaj-Lanhsen anwsered you can change the data attributes.像Hugo Elhaj-Lanhsen anwsered 一样,您可以更改数据属性。 I would like to add how to do that depending on the viewport size, using Window.matchMedia and MediaQuery.addListener() in JavaScript.
我想根据视口大小添加如何执行此操作,在 JavaScript 中使用Window.matchMedia和MediaQuery.addListener() 。
const mqList = window.matchMedia("(max-width: 419px)")
// If media query matches on load
if (mqList.matches) {
setDelay(400) // using Hugo's function
}
// If media query matches after resize
mqList.addListener(function(mql) {
if (mql.matches) {
setDelay(400) // using Hugo's function
}
})
And make sure to add the HTML viewport meta tag so the viewport is the size of the device.并确保添加 HTML 视口元标记,以便视口为设备的大小。
<meta name="viewport" content="width=device-width, initial-scale=1">
In HTML, the key-value pairs like class="name"
are known as attributes .在 HTML 中,像
class="name"
这样的键值对被称为属性。 class
, style
and src
are examples of attributes. class
、 style
和src
是属性的例子。
Attributes prefixed with data-
are known as data attributes .以
data-
为前缀的属性称为数据属性。 Data attributes, unlike normal attributes which are defined in HTML directly, can be defined by the programmer .数据属性不同于在 HTML 中直接定义的普通属性,可以由程序员定义。 This is why your library uses a data attribute in order to set the delay.
这就是您的库使用数据属性来设置延迟的原因。
To set an attribute's value , you can use Element.setAttribute(name, value) .要设置属性的值,您可以使用Element.setAttribute(name, value) 。 In this case, we would get the element and set its
data-aos-delay
data attribute:在这种情况下,我们将获取元素并设置其
data-aos-delay
数据属性:
function setDelay(number) {
document.querySelector('.skill').setAttribute('data-aos-delay', number)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.