英文:
Javascript trigger (progressbar) when on screen
问题
I am currently making my own 1-page portfolio website for school, and I had this cool idea that I want to show how far I've become in some coding languages by using a progress bar. For example, C# = 60%, and that would be 60% of a circle. I think I can do that by using the internet, but I would like it to start/end at 60% when I'm scrolling down, and it should start the progress bar when it is on the screen. I do not know and cannot find how to trigger the code when the progress bar is visible on the screen.
Note: I am a fairly new JavaScript programmer and a fairly new user, so please explain if I did something wrong.
英文:
i am currently making my own 1-page portfolio website for school and i had this cool idea that i want to show how far ive become in some coding languages by using a progressbar, for example, c# = 60% and that would be 60% of a circle. i think i can do that bu using the internet but i would like it to start / end at 60 when i'm scrolling down and it should start the progressbar when it is on screen. i do not know and cannot find on how to trigger the code when the (in this case progressbar) is visible on screen.
note: i am a fairly new javascript programmer and a fairly new user so please explain if i did something wrong.
答案1
得分: 1
以下是使用Intersection Observer API在滚动到图像高度的75%时触发动画的工作示例:
<!-- 开始片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-js -->
const images = document.querySelectorAll('.animate-me');
const config = {
root: null, // 视口
rootMargin: '0px',
threshold: 0.75
};
let observer = new IntersectionObserver(function(entries) {
entries.forEach(function(entry) {
if (entry.intersectionRatio >= config.threshold) {
entry.target.classList.add("active");
}
});
}, config);
images.forEach(image => {
observer.observe(image);
});
<!-- 语言:lang-css -->
.spacer {
height: 400px;
}
.animate-me.active {
animation: anim1 .7s ease-out;
}
@keyframes anim1 {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
<!-- 语言:lang-html -->
<div class="spacer">(向下滚动)</div>
<img src="https://picsum.photos/200" class="animate-me">
<div class="spacer"></div>
<!-- 结束片段 -->
您应该能够将此示例翻译为您自己的用例,但如果您寻找其他内容,请分享更多具体信息。
英文:
Here is a working example of triggering an animation when you scroll to 75% of an image's height using the Intersection Observer API:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const images = document.querySelectorAll('.animate-me');
const config = {
root: null, // viewport
rootMargin: '0px',
threshold: 0.75
};
let observer = new IntersectionObserver(function(entries) {
entries.forEach(function(entry) {
if (entry.intersectionRatio >= config.threshold) {
entry.target.classList.add("active");
}
});
}, config);
images.forEach(image => {
observer.observe(image);
});
<!-- language: lang-css -->
.spacer {
height: 400px;
}
.animate-me.active {
animation: anim1 .7s ease-out;
}
@keyframes anim1 {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
<!-- language: lang-html -->
<div class="spacer">(Scroll down)</div>
<img src="https://picsum.photos/200" class="animate-me">
<div class="spacer"></div>
<!-- end snippet -->
You should be able to translate this to your own use case, but please share more specifics if you are looking for something else.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论