I am building a page that I want to have multiple animations fire when they come into view. Originally I was using the same scrollFunction and just repeating the whole function every time for each element I wanted animated. Trying to avoid writing the same function over and over so I adjusted it a bit so all I had to do is call the function with different arguments. I have it working but if I call it more than 4 times any animation after that fires immediately. All the ones before work just fine and only fire when in view. I have changed class names, animation names, tried it on different elements on different parts of the page, still fires immediately. Also I can not use any jquery past v1.11 because the editor I work with has not been updated and I can not use a cdn. HTML for one of the animations that works and one that fires immediately:
// this works fine
<div class="hero-tag">
<h1 class="heroTitle">WOLF</h1>
<h3 class="heroSubTitle font-thin">Kitchen and Bath Cabinets</h3>
</div>
// spacer added for code not associated with animations
<div style="height500px;"</div>
<div id="advantage">
<div class="advantage-grid">
<h2 id="grid-header">the <span class="heavy-sm underline">wolf</span> advantage</h2>
<div class="grid-item grid-item1">
<svg class="protectionSvg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 152.91 152.23">
<g class="protectionGroup"><g class="protectionGroup">
<path class="protectionCircle circle" d="M150.8,67.46c-3.7-34.2-30.3-59.9-63.5-65.1h.2c-.6-.8-1.7,0-2.3-.7H82.9c.1.1.3.1.4.2-.6-.1-1.2-.1-1.8-.2-.2.4-.4.4-.7,0H72.1c-.2.4-.4.4-.7,0a10.87,10.87,0,0,0-1.8.1c.1,0,.1-.1.2-.1h-2c-.1.1-.2.2-.3.2a2,2,0,0,1-.8.1c-.3,0-.6.1-.9.1C33.3,6,5,34.26,1.8,67.06h0a1.21,1.21,0,0,1-.3,1v17.3c.8.6-.1,1.7.7,2.3C17.9,183.86,161.9,164.36,150.8,67.46Z" />
<path class="protectionPath inner" d="M74.4,115.26c0,1.4-.2,1.9-1.7,1.2-30.5-10.5-28-42.3-28.1-68.9,0-.9.3-1.2,1.2-1.4L73,39c1.4-.4,1.4.3,1.4,1.3ZM108.2,48c-.1,26.6,2.2,58.3-28.4,68.6-1.5.6-1.3-.3-1.3-1.2,0-25,.1-50,0-75,0-1.5.4-1.7,1.8-1.3,8.8,2.4,17.7,4.7,26.6,7A1.57,1.57,0,0,1,108.2,48Zm-33.8,67.3c0,1.4-.2,1.9-1.7,1.2-30.5-10.5-28-42.3-28.1-68.9,0-.9.3-1.2,1.2-1.4L73,39c1.4-.4,1.4.3,1.4,1.3ZM108.2,48c-.1,26.6,2.2,58.3-28.4,68.6-1.5.6-1.3-.3-1.3-1.2,0-25,.1-50,0-75,0-1.5.4-1.7,1.8-1.3,8.8,2.4,17.7,4.7,26.6,7A1.57,1.57,0,0,1,108.2,48Zm-33.8,67.3c0,1.4-.2,1.9-1.7,1.2-30.5-10.5-28-42.3-28.1-68.9,0-.9.3-1.2,1.2-1.4L73,39c1.4-.4,1.4.3,1.4,1.3ZM108.2,48c-.1,26.6,2.2,58.3-28.4,68.6-1.5.6-1.3-.3-1.3-1.2,0-25,.1-50,0-75,0-1.5.4-1.7,1.8-1.3,8.8,2.4,17.7,4.7,26.6,7A1.57,1.57,0,0,1,108.2,48Z" ></g></g>
</svg>
<p>Our culture of continuous improvement keeps us on the leading edge to ensure satisfactory
performance. </p>
</div>
<div class="grid-item grid-item2">
<svg class="qualitySvg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 152.92 152.89">
<g id="Layer_2" data-name="Layer 2">
<g class="qualityGroup">
<path class="qualityCircle circle" d="M151.42,82.8c-.7,1.8-.6,3.7-.9,5.5a75,75,0,0,1-62.2,62.2c-39.3,6-76.2-18.9-84.9-57.4A74.8,74.8,0,0,1,59.53,3.42c1.75-.4,3.52-.74,5.29-1s3.6-.2,5.3-.9h12.3c3.2.4,6.4.4,9.5,1.1a74.19,74.19,0,0,1,34.5,17.5c13,11.5,20.9,25.8,24.2,42.8.5,2.5.3,5.1.8,7.6V82.8Z" fill="none" stroke="#56868d" stroke-miterlimit="10" stroke-width="3"/>
<path class="qualityPath inner" d="M93.72,50.4H118c1.4,0,1.7.4,1.7,1.8v48.6c0,1.4-.4,1.8-1.8,1.8H69.12c-1.3,0-1.8-.3-1.7-1.6q.15-3,0-6c0-.9.2-1.3,1.1-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.8,1.2,1.1.4,2.2.7,3.3,1.1a6.2,6.2,0,0,1,4.2,6.5,6.68,6.68,0,0,1-4.5,6.5A13.11,13.11,0,0,1,99.82,89.1ZM59.5,89c-.7,0-1.1.1-1.6.1H30.12c-1.4,0-1.8-.3-1.8-1.7.1-2.7,0-2.7,2.7-2.7h23.6a2.42,2.42,0,0,1,2.5,1.4C57.82,87,58.62,87.9,59.5,89ZM45.62,74.2h7.6c.9,0,1.2.2,1.2,1.2,0,3.3,0,3.3-3.3,3.3H38c-1.3,0-1.7-.3-1.7-1.6.1-2.9,0-2.9,2.9-2.9Zm13.8-10.3c-.9,1.3-1.8,2.5-2.6,3.7-.3.5-.8.5-1.3.5H45c-.9,0-1.2-.3-1.2-1.2-.1-3.2-.1-3.2,3.1-3.2H59C59,63.8,59.12,63.8,59.42,63.9Z"/></g></g>
</svg>
<p>Vast inventories and smart logistics solutions combine to get you the products you need when you need
them. Plus, select products from Wolf are available with Quick Ship for delivery in days, not weeks.
</p>
</div>
<div class="grid-item grid-item3">
<svg class="ribbonSvg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 152 152.26">
<g class="layer2" data-name="Layer 2">
<g class="ribbonGroup">
<path class="ribbonCircle circle" d="M150.46,74.23c0-28.2-18.27-55-44-66.33C55.72-14.35-1.18,24.28,1.6,79.79c1.29,45.38,47.57,80.14,91.56,68.82C126.82,141.06,151.65,108.69,150.46,74.23Z" />
<path class="ribbonPath inner" d="M92.36,87.24v27.31c-.39,1.88-2.28-.3-3.07-.8l-11.62-8.94a2.47,2.47,0,0,0-3.38,0l-12.81,9.84c-.4.29-.79.79-1.39.49s-.4-.89-.4-1.49V86.44a28.53,28.53,0,0,0,32.58.1A2.92,2.92,0,0,1,92.36,87.24ZM76,44.74C51.45,45.43,51.45,81.38,76,82,100.61,81.48,100.51,45.43,76,44.74ZM64.46,69.16c-.5,0-.4-.29-.4-.59V58.24c0-.3,0-.5.5-.5s.4.3.4.6V68.67C65,68.87,65,69.16,64.46,69.16ZM74,68.67a5.9,5.9,0,0,1-6.65-.9c-.2-.19-.4-.29-.1-.59s.4-.4.69-.1a5.4,5.4,0,0,0,4.87,1.29,2.09,2.09,0,0,0,.8-3.87c-1.39-.8-3.08-.7-4.47-1.39a2.72,2.72,0,0,1-.3-4.77c1.59-1.19,4.47-.89,6.16.5.29.2,0,.3-.1.49s-.3.4-.6.1a4.39,4.39,0,0,0-4.87-.59,2,2,0,0,0,.1,3.47c1.39.7,3,.7,4.37,1.39C76,64.7,75.68,67.58,74,68.67Zm8.74.69c-7.55,0-7.64-11.71,0-11.81S90.28,69.26,82.73,69.36Zm2.78-10.22a4.47,4.47,0,0,0-5-.3c-5.37,2.78-1.69,11.12,3.37,9.53C88.09,67.48,88.89,61.42,85.51,59.14ZM76,36.79c-35.16.9-35.06,52.43.1,53.23C111,89,111,37.79,76,36.79Zm.3,47.07c-27-.3-27.61-39.72-.6-40.81C102.79,43.45,103,82.77,76.28,83.86Zm16.08,3.38v27.31c-.39,1.88-2.28-.3-3.07-.8l-11.62-8.94a2.47,2.47,0,0,0-3.38,0l-12.81,9.84c-.4.29-.79.79-1.39.49s-.4-.89-.4-1.49V86.44a28.53,28.53,0,0,0,32.58.1A2.92,2.92,0,0,1,92.36,87.24ZM76,44.74C51.45,45.43,51.45,81.38,76,82,100.61,81.48,100.51,45.43,76,44.74ZM64.46,69.16c-.5,0-.4-.29-.4-.59V58.24c0-.3,0-.5.5-.5s.4.3.4.6V68.67C65,68.87,65,69.16,64.46,69.16ZM74,68.67a5.9,5.9,0,0,1-6.65-.9c-.2-.19-.4-.29-.1-.59s.4-.4.69-.1a5.4,5.4,0,0,0,4.87,1.29,2.09,2.09,0,0,0,.8-3.87c-1.39-.8-3.08-.7-4.47-1.39a2.72,2.72,0,0,1-.3-4.77c1.59-1.19,4.47-.89,6.16.5.29.2,0,.3-.1.49s-.3.4-.6.1a4.39,4.39,0,0,0-4.87-.59,2,2,0,0,0,.1,3.47c1.39.7,3,.7,4.37,1.39C76,64.7,75.68,67.58,74,68.67Zm8.74.69c-7.55,0-7.64-11.71,0-11.81S90.28,69.26,82.73,69.36Zm2.78-10.22a4.47,4.47,0,0,0-5-.3c-5.37,2.78-1.69,11.12,3.37,9.53C88.09,67.48,88.89,61.42,85.51,59.14ZM76,36.79c-35.16.9-35.06,52.43.1,53.23C111,89,111,37.79,76,36.79Zm.3,47.07c-27-.3-27.61-39.72-.6-40.81C102.79,43.45,103,82.77,76.28,83.86ZM76,44.74C51.45,45.43,51.45,81.38,76,82,100.61,81.48,100.51,45.43,76,44.74ZM64.46,69.16c-.5,0-.4-.29-.4-.59V58.24c0-.3,0-.5.5-.5s.4.3.4.6V68.67C65,68.87,65,69.16,64.46,69.16ZM74,68.67a5.9,5.9,0,0,1-6.65-.9c-.2-.19-.4-.29-.1-.59s.4-.4.69-.1a5.4,5.4,0,0,0,4.87,1.29,2.09,2.09,0,0,0,.8-3.87c-1.39-.8-3.08-.7-4.47-1.39a2.72,2.72,0,0,1-.3-4.77c1.59-1.19,4.47-.89,6.16.5.29.2,0,.3-.1.49s-.3.4-.6.1a4.39,4.39,0,0,0-4.87-.59,2,2,0,0,0,.1,3.47c1.39.7,3,.7,4.37,1.39C76,64.7,75.68,67.58,74,68.67Zm8.74.69c-7.55,0-7.64-11.71,0-11.81S90.28,69.26,82.73,69.36Zm2.78-10.22a4.47,4.47,0,0,0-5-.3c-5.37,2.78-1.69,11.12,3.37,9.53C88.09,67.48,88.89,61.42,85.51,59.14ZM76,36.79c-35.16.9-35.06,52.43.1,53.23C111,89,111,37.79,76,36.79Zm.3,47.07c-27-.3-27.61-39.72-.6-40.81C102.79,43.45,103,82.77,76.28,83.86Zm16.08,30.29c.1.79-.39,1.69-1.29,1.09-4.47-3.38-8.93-6.85-13.4-10.23a2.47,2.47,0,0,0-3.38,0l-12.81,9.83c-.4.3-.79.8-1.39.5s-.4-.89-.4-1.49V86.64a28.57,28.57,0,0,0,32.58.1C92.56,95.78,92.27,105,92.36,114.15ZM76,44.74C51.45,45.43,51.45,81.38,76,82,100.61,81.48,100.51,45.43,76,44.74ZM64.46,69.16c-.5,0-.4-.29-.4-.59V58.24c0-.3,0-.5.5-.5s.4.3.4.6V68.67C65,68.87,65,69.16,64.46,69.16ZM74,68.67a5.9,5.9,0,0,1-6.65-.9c-.2-.19-.4-.29-.1-.59s.4-.4.69-.1a5.4,5.4,0,0,0,4.87,1.29,2.09,2.09,0,0,0,.8-3.87c-1.39-.8-3.08-.7-4.47-1.39a2.72,2.72,0,0,1-.3-4.77c1.59-1.19,4.47-.89,6.16.5.29.2,0,.3-.1.49s-.3.4-.6.1a4.39,4.39,0,0,0-4.87-.59,2,2,0,0,0,.1,3.47c1.39.7,3,.7,4.37,1.39C76,64.7,75.68,67.58,74,68.67Zm8.74.69c-7.55,0-7.64-11.71,0-11.81S90.28,69.26,82.73,69.36Zm2.78-10.22a4.47,4.47,0,0,0-5-.3c-5.37,2.78-1.69,11.12,3.37,9.53C88.09,67.48,88.89,61.42,85.51,59.14Zm-21.05,10c-.5,0-.4-.29-.4-.59V58.24c0-.3,0-.5.5-.5s.4.3.4.6V68.67C65,68.87,65,69.16,64.46,69.16ZM74,68.67a5.9,5.9,0,0,1-6.65-.9c-.2-.19-.4-.29-.1-.59s.4-.4.69-.1a5.4,5.4,0,0,0,4.87,1.29,2.09,2.09,0,0,0,.8-3.87c-1.39-.8-3.08-.7-4.47-1.39a2.72,2.72,0,0,1-.3-4.77c1.59-1.19,4.47-.89,6.16.5.29.2,0,.3-.1.49s-.3.4-.6.1a4.39,4.39,0,0,0-4.87-.59,2,2,0,0,0,.1,3.47c1.39.7,3,.7,4.37,1.39C76,64.7,75.68,67.58,74,68.67Zm8.74.69c-7.55,0-7.64-11.71,0-11.81S90.28,69.26,82.73,69.36Zm2.78-10.22a4.47,4.47,0,0,0-5-.3c-5.37,2.78-1.69,11.12,3.37,9.53C88.09,67.48,88.89,61.42,85.51,59.14Z"/></g></g></svg>
<p>We offer premium products without the premium price. Our high-quality products are engineered to
provide unmatched value and are backed by industry-leading warranties.</p>
<!-- </div> -->
</div>
</div>
</div>
// spacer added for code not associated with animations
<div style="height500px;"</div>
<div class='styles'>
<span class="sub"><p class="stylesItem"> Classic</p></span>
<span class="sub"><p class="stylesItem"> Signature</p></span>
<span class="sub"><p class="stylesItem"> Endeavor</p></span>
<span class="sub"><p class="stylesItem"> Designer</p></span>
<span class="sub"><p class="stylesItem"> Builders-Mix</p></span>
<h1 class="title">WOLF kitchen cabinets</h1>
</div>
// spacer added for code not associated with animations
<div style="height500px;"</div>
// This is much further down the page but fires as soon as I start scrolling
<div class="bath-divider">
<h1 class="callBath">WOLF</h1>
<h3 class="font-thin callBathSub">Bathroom Vanity Tops</h3>
</div>
// spacer added for code not associated with animations
<div style="height500px;"</div>
//This works fine
<div class='styles'>
<span class="sub"><p class="stylesItem"> Solid Surface</p></span>
<span class="sub"><p class="stylesItem"> Quartz</p></span>
<span class="sub"><p class="stylesItem"> Natural Granite</p></span>
<span class="sub"><p class="stylesItem"> Cultured Marble</p></span>
<h1 class="title">WOLF bathroom vanity tops</h1>
</div>
Animations and CSS:
// This works Fine
.protectionSvg{
width:150px;
height:150px;
}
.protectionPath{
max-width:150px;
fill:#56868D;
stroke-width:1px;
}
.protectionSvg{
fill:transparent;
}
.protectionPath{
stroke:#56868D;
fill:none;
stroke-width:3px;
stroke-dasharray:650;
stroke-dashoffset:650;
}
.protectionCircle{
stroke:#56868D;
fill:none;
stroke-width:3px;
stroke-dasharray:650;
stroke-dashoffset:650;
}
.qualitySvg{
width:150px;
width:150px;
}
.qualityCircle{
fill:none;
stroke:#56868D;
stroke-width:3px;
stroke-dasharray:650;
stroke-dashoffset:650;
}
.qualityPath{
fill:none;
stroke:#56868D;
stroke-width:1px;
stroke-dasharray:650;
stroke-dashoffset:650;
}
.ribbonSvg{
width:150px;
height:150px;
}
.ribbonPath{
fill:none;
stroke:#56868D;
stroke-width:1;
stroke-dasharray:650;
stroke-dashoffset:650;
}
.ribbonCircle{
fill:none;
stroke:#56868D;
stroke-width:3px;
stroke-dasharray:650;
stroke-dashoffset:650;
}
.ribbonGroup{
stroke-dasharray:650;
stroke-dashoffset:0;
}
.innerAnim{
animation: inner-anim 2s 1.5s linear forwards;
-webkit-animation: inner-anim 2s 1.5s linear forwards;
}
.circleAnim{
stroke-dasharray:650;
stroke-dashoffset:650;
animation: circle-anim 2s linear forwards;
-webkit-animation: circle-anim 2s linear forwards;
}
@keyframes circle-anim{
0%{
stroke-dashoffset:650;
}
100%{
stroke-dashoffset:0;
}
}
@-webkit-keyframes circle-anim{
0%{
stroke-dashoffset:650;
}
100%{
stroke-dashoffset:0;
}
}
@keyframes inner-anim{
0%{
stroke-dashoffset:650;
fill:transparent;
}
100%{
stroke-dashoffset:0;
fill:#FFE09C;
}
}
@-webkit-keyframes inner-anim{
0%{
stroke-dashoffset:650;
fill:transparent;
}
100%{
stroke-dashoffset:0;
fill:#FFE09C;
}
}
.heroTitle{
display:inline-block;
position: relative;
transform:scale(0);
animation: hero 1s linear forwards;
-webkit-animation: hero 1s linear forwards;
}
.heroTitle::after{
content: "";
position: absolute;
top:50%;
right:-25%;
height:2px;
width:0;
background-color:#ececec;
animation: sidelines .5s 1.5s linear forwards;
-webkit-animation: sidelines .5s 1.5s linear forwards;
}
.heroTitle::before{
content: "";
position: absolute;
top:50%;
left:-25%;
height:2px;
width:0;
background-color:#ececec;
animation: sidelines .5s 1.5s linear forwards;
-webkit-animation: sidelines .5s 1.5s linear forwards;
}
.heroSubTitle{
animation: hero 1s .1s linear forwards;
-webkit-animation: hero 1s .1s linear forwards;
}
@keyframes hero {
from{
transform:scaleX(0);
}
to{
transform:scaleX(1);
}
}
@-webkit-keyframes hero {
from{
transform:scaleX(0);
}
to{
transform:scaleX(1);
}
}
@keyframes sidelines {
to{
width:25%;
}
}
@-webkit-keyframes sidelines {
to{
width:25%;
}
}
.subTitle{
position: relative;
font-size:2.2rem;
line-height:55px;
}
.subTitle::before{
content:"";
position: absolute;
top:5px;
left:-10px;
height:0;
width:2px;
background:#000;
animation: sub-title-lines 1s linear forwards;
-webkit-animation: sub-title-lines 1s linear forwards;
}
.subTitleAnim{
display:inline-block;
font-weight:200;
margin-right:35px;
transform:scaleX(0);
transform-origin:left;
animation: sub-title-anim 1s 1s linear forwards;
-webkit-animation: sub-title-anim 1s 1s linear forwards;
}
@keyframes sub-title-lines {
from{
height:0%;
}
to{
height:90%;
}
}
@-webkit-keyframes sub-title-lines {
from{
height:0%;
}
to{
height:90%;
}
}
@keyframes sub-title-anim {
to{
transform:scaleX(1);
}
}
@-webkit-keyframes sub-title-anim {
to{
transform:scaleX(1);
}
}
// This fires immediately
.bathTitleAnim{
animation: bathTitleAnim 2s forwards;
-webkit-animation: bathTitleAnim 2s forwards;
}
.bathSubAnim{
animation: bathTitleAnim 2s forwards;
-webkit-animation: bathTitleAnim 2s forwards;
}
@keyframes bathTitleAnim{
from{
transform:scale(0);
}
to{
transform:scale(1);
}
}
@-webkit-keyframes bathTitleAnim{
from{
transform:scale(0);
}
to{
transform:scale(1);
}
}
$(document).ready(function () {
let scrollFunction = function (elem, anim) {
$(window).scroll(function () {
$(elem).each(function (i) {
var bottom_of_object = $(this).position().top;
var bottom_of_window = $(window).scrollTop() + $(window).height();
if (bottom_of_window > bottom_of_object) {
return $(this).addClass(anim)
}
});
});
}
scrollFunction('.circle', 'circleAnim');
scrollFunction('.inner', 'innerAnim');
scrollFunction('.sub', 'subTitle');
scrollFunction('.stylesItem', 'subTitleAnim');
scrollFunction('.callBath', 'bathTitleAnim'); // fires immediately
scrollFunction('.callBathSub', 'bathSubAnim'); // fires immediately
});