Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Guide] How to use .wow (onScroll) animations #7

Open
rallisf1 opened this issue Jul 18, 2020 · 0 comments
Open

[Guide] How to use .wow (onScroll) animations #7

rallisf1 opened this issue Jul 18, 2020 · 0 comments

Comments

@rallisf1
Copy link
Contributor

CSS transitions work out of the box but I wanted to animate some icons when they appear on screen so I used the following code

<script>
  import MDBIcon from "mdbsvelte/src/MDBIcon.svelte";
  import { onMount } from 'svelte'

  let bodyHeight = 0
  let hasScrolled = false
  let waitingOnAnimRequest = false

  onMount(() => {
    bodyHeight = window.innerHeight
		|| document.documentElement.clientHeight
		|| document.body.clientHeight;
  })

  const animChecker = target => {
    target.querySelectorAll('[data-wow]').forEach(element => {
	  const elementTop = element.getBoundingClientRect().top
	  console.log(elementTop);
	  console.log(bodyHeight);
      if (elementTop < bodyHeight * 0.8) {
        if (!element.classList.contains('animated')) {
          element.classList.add('animated',element.dataset.wow)
        }
      }
    })
  }

  const onScroll = ({ target }) => {
    if (!waitingOnAnimRequest) {
      window.requestAnimationFrame(() => {
        animChecker(target)
        waitingOnAnimRequest = false
      })
      waitingOnAnimRequest = true
    }

    hasScrolled = document.body.scrollTop !== 0
  }
 
</script>

<style>
:global([data-wow]) {
  visibility: hidden;
}
:global(.animated) {
  visibility: visible!important;
}
</style>

<svelte:window on:scroll="{onScroll}" />

<MDBIcon fab icon="opencart" size="4x" data-wow="bounceInLeft" />

You can use any of the available CSS transitions as a data-wow attribute.

Tip: You can convert this into a a svelte component and import it wherever or maybe the [Author] @SauravKanchan wants to integrate into the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant