触发 JavaScript 进度条在屏幕上可见时。

huangapple go评论195阅读模式
英文:

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(&#39;.animate-me&#39;);

const config = {
  root: null, // viewport
  rootMargin: &#39;0px&#39;,
  threshold: 0.75
};

let observer = new IntersectionObserver(function(entries) {
  entries.forEach(function(entry) {
    if (entry.intersectionRatio &gt;= config.threshold) {
      entry.target.classList.add(&quot;active&quot;);
    }
  });
}, config);

images.forEach(image =&gt; {
  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 -->

&lt;div class=&quot;spacer&quot;&gt;(Scroll down)&lt;/div&gt;
&lt;img src=&quot;https://picsum.photos/200&quot; class=&quot;animate-me&quot;&gt;
&lt;div class=&quot;spacer&quot;&gt;&lt;/div&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2020年1月7日 02:14:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定