- This topic has 7 replies, 2 voices, and was last updated 3 years, 9 months ago by Elvin.
-
AuthorPosts
-
December 27, 2020 at 4:09 pm #1596087Kai
I have been searching around and do some trial-and-error, but no luck. I am looking for something like the second answer in this post: https://stackoverflow.com/questions/26694385/fade-in-on-scroll-down-fade-out-on-scroll-up-based-on-element-position-in-win
My site is nguyen-kai.com – as you can see, it is pretty basic.
December 27, 2020 at 5:52 pm #1596129ElvinStaffCustomer SupportHi,
How are you adding the script?
You can use Hook element to place your script.
https://docs.generatepress.com/article/hooks-element-overview/You can copy the known working script and place it within the Hook element’s code area wrapped within a
<script>...</script>
You then hook it to
wp_footer
.December 27, 2020 at 6:22 pm #1596131KaiI see. So what I did was the following but it did not work out:
1. Go to Dashboard >> Appearance >> Elements >> “Add New Element”.
2. When prompted to choose the element type, I choose “Hook”.
3. In the code section, I added:
<script>
$(window).on(“load”,function() {
$(window).scroll(function() {
var windowBottom = $(this).scrollTop() + $(this).innerHeight();
$(“.fade”).each(function() {
/* Check the location of each desired element */
var objectBottom = $(this).offset().top + $(this).outerHeight();/* If the element is completely within bounds of the window, fade it in */
if (objectBottom < windowBottom) { //object comes into view (scrolling down)
if ($(this).css(“opacity”)==0) {$(this).fadeTo(500,1);}
} else { //object goes out of view (scrolling up)
if ($(this).css(“opacity”)==1) {$(this).fadeTo(500,0);}
}
});
}).scroll(); //invoke scroll-handler on page-load
});
</script>4. I open my website, nguyen-kai.com, then open Additional CSS, and pasted this:
.fade {
margin: 50px;
padding: 50px;
background-color: lightgreen;
opacity: 1;
}December 27, 2020 at 7:56 pm #1596158ElvinStaffCustomer SupportHi,
You’re site has a error. This line is causing it:
if (objectBottom < windowBottom)
Fix
<
and place<
.Also, you’re referencing
.fade
class from$(".fade")
but there seems to be no element using it so the animation is not applying on anything.You can try adding
fade
class on your GenerateBlocks Container Blocks through its advanced settings.December 27, 2020 at 8:59 pm #1596212KaiAh wow I did not even notice both of those! Thank you. Unfortunately, I think that since I use jQuery, it is not compatible with WordPress or at least my site somehow. As I am on my site, I am seeing an error saying that “$ is not a function”.
December 27, 2020 at 9:12 pm #1596216ElvinStaffCustomer SupportAs I am on my site, I am seeing an error saying that “$ is not a function”.
Ah right of course. My bad. I forgot to mention that you’ll have to change all of the
$
tojQuery
.Or keep it but make sure you use
.noConflict()
. (this may be confusing, usingjQuery
instead of$
should work.)
https://api.jquery.com/jquery.noconflict/Reminder: I still don’t see the use of
fade
class in any of your elements. This script won’t do anything unless there’s an element withfade
class within your page.December 27, 2020 at 9:40 pm #1596230KaiThank you so much!
December 27, 2020 at 11:57 pm #1596315ElvinStaffCustomer SupportNo problem. 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.