Make function to animate when in view more flexible












1














I have some code to animate a couple of numbers when scrolled into view. Everything is working fine. However, currently it only works with ONE set of numbers and tied to ONE div (with an id). I'd like to make it become more flexible, making it work for more than 1 sets of numbers.The DIV ids can be counter1, counter2, counter3 etc.



var a = 0;
$(window).scroll(function() {
var oTop = $('#counter1').offset().top - window.innerHeight;
if (a == 0 && $(window).scrollTop() > oTop) {
$('.counter-value').each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
...


Here's the pen:
https://codepen.io/anon/pen/bQWpjJ



Thanks for you help!










share|improve this question






















  • What do you mean by " making it work for more than 1 sets of numbers." ?
    – Dacre Denny
    Nov 15 '18 at 1:39










  • in the code the numbers are inside <div id="counter1">. I have more set of numbers inside <div id="counter2">, <div id="counter3"> etc. I've also added the second set of numbers to codepen.I wanted to animate all sets of number using the same effect.
    – User123
    Nov 15 '18 at 1:50


















1














I have some code to animate a couple of numbers when scrolled into view. Everything is working fine. However, currently it only works with ONE set of numbers and tied to ONE div (with an id). I'd like to make it become more flexible, making it work for more than 1 sets of numbers.The DIV ids can be counter1, counter2, counter3 etc.



var a = 0;
$(window).scroll(function() {
var oTop = $('#counter1').offset().top - window.innerHeight;
if (a == 0 && $(window).scrollTop() > oTop) {
$('.counter-value').each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
...


Here's the pen:
https://codepen.io/anon/pen/bQWpjJ



Thanks for you help!










share|improve this question






















  • What do you mean by " making it work for more than 1 sets of numbers." ?
    – Dacre Denny
    Nov 15 '18 at 1:39










  • in the code the numbers are inside <div id="counter1">. I have more set of numbers inside <div id="counter2">, <div id="counter3"> etc. I've also added the second set of numbers to codepen.I wanted to animate all sets of number using the same effect.
    – User123
    Nov 15 '18 at 1:50
















1












1








1







I have some code to animate a couple of numbers when scrolled into view. Everything is working fine. However, currently it only works with ONE set of numbers and tied to ONE div (with an id). I'd like to make it become more flexible, making it work for more than 1 sets of numbers.The DIV ids can be counter1, counter2, counter3 etc.



var a = 0;
$(window).scroll(function() {
var oTop = $('#counter1').offset().top - window.innerHeight;
if (a == 0 && $(window).scrollTop() > oTop) {
$('.counter-value').each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
...


Here's the pen:
https://codepen.io/anon/pen/bQWpjJ



Thanks for you help!










share|improve this question













I have some code to animate a couple of numbers when scrolled into view. Everything is working fine. However, currently it only works with ONE set of numbers and tied to ONE div (with an id). I'd like to make it become more flexible, making it work for more than 1 sets of numbers.The DIV ids can be counter1, counter2, counter3 etc.



var a = 0;
$(window).scroll(function() {
var oTop = $('#counter1').offset().top - window.innerHeight;
if (a == 0 && $(window).scrollTop() > oTop) {
$('.counter-value').each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
...


Here's the pen:
https://codepen.io/anon/pen/bQWpjJ



Thanks for you help!







javascript jquery






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 1:33









User123User123

296




296












  • What do you mean by " making it work for more than 1 sets of numbers." ?
    – Dacre Denny
    Nov 15 '18 at 1:39










  • in the code the numbers are inside <div id="counter1">. I have more set of numbers inside <div id="counter2">, <div id="counter3"> etc. I've also added the second set of numbers to codepen.I wanted to animate all sets of number using the same effect.
    – User123
    Nov 15 '18 at 1:50




















  • What do you mean by " making it work for more than 1 sets of numbers." ?
    – Dacre Denny
    Nov 15 '18 at 1:39










  • in the code the numbers are inside <div id="counter1">. I have more set of numbers inside <div id="counter2">, <div id="counter3"> etc. I've also added the second set of numbers to codepen.I wanted to animate all sets of number using the same effect.
    – User123
    Nov 15 '18 at 1:50


















What do you mean by " making it work for more than 1 sets of numbers." ?
– Dacre Denny
Nov 15 '18 at 1:39




What do you mean by " making it work for more than 1 sets of numbers." ?
– Dacre Denny
Nov 15 '18 at 1:39












in the code the numbers are inside <div id="counter1">. I have more set of numbers inside <div id="counter2">, <div id="counter3"> etc. I've also added the second set of numbers to codepen.I wanted to animate all sets of number using the same effect.
– User123
Nov 15 '18 at 1:50






in the code the numbers are inside <div id="counter1">. I have more set of numbers inside <div id="counter2">, <div id="counter3"> etc. I've also added the second set of numbers to codepen.I wanted to animate all sets of number using the same effect.
– User123
Nov 15 '18 at 1:50














1 Answer
1






active

oldest

votes


















1














Perhaps you could revise your implementation by first iterating over each counter using $('#counter1, #counter2').each( .. ).



Inside of each iteration, you'd effectively re-use your existing code by setting up a window.scroll() handler for that counter instance.



You'd also move var a = 0 inside of the iteration, so that your code tracks the unique scroll offset for this counter instance.



Finally, you'd want to ensure that you select '.counter-value' elements for the current counter instance of the iteration:






$('#counter1, #counter2').each(function() {

var a = 0;
var counter = $(this);

$(window).scroll(function() {

var oTop = counter.offset().top - window.innerHeight;

if (a == 0 && $(window).scrollTop() > oTop) {

$('.counter-value', counter).each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({
countNum: $this.text()
}).animate({
countNum: countTo
},

{

duration: 2000,
easing: 'swing',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}

});
});

a = 1;
}

});

})

.spacing {
width:100%;
height: 1280px;
position:relative;
}
.counter {text-align:center}
.counter-value {display:inline-block; padding:20px 40px; margin:0 20px; border:1px solid #ddd; font-family:Arial; font-size:50px; font-weight:bold}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="spacing"></div>
<div id="counter1">
<div class="counter-value" data-count="300">0</div>
<div class="counter-value" data-count="400">100</div>
<div class="counter-value" data-count="1500">200</div>
</div>
<div class="spacing"></div>

<div id="counter2">
<div class="counter-value" data-count="500">100</div>
<div class="counter-value" data-count="600">200</div>
<div class="counter-value" data-count="1700">300</div>
</div>
<div class="spacing"></div>





Here's a working codepen - hope that helps!






share|improve this answer























  • @User123 you're welcome :)
    – Dacre Denny
    Nov 15 '18 at 2:13











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311211%2fmake-function-to-animate-when-in-view-more-flexible%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Perhaps you could revise your implementation by first iterating over each counter using $('#counter1, #counter2').each( .. ).



Inside of each iteration, you'd effectively re-use your existing code by setting up a window.scroll() handler for that counter instance.



You'd also move var a = 0 inside of the iteration, so that your code tracks the unique scroll offset for this counter instance.



Finally, you'd want to ensure that you select '.counter-value' elements for the current counter instance of the iteration:






$('#counter1, #counter2').each(function() {

var a = 0;
var counter = $(this);

$(window).scroll(function() {

var oTop = counter.offset().top - window.innerHeight;

if (a == 0 && $(window).scrollTop() > oTop) {

$('.counter-value', counter).each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({
countNum: $this.text()
}).animate({
countNum: countTo
},

{

duration: 2000,
easing: 'swing',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}

});
});

a = 1;
}

});

})

.spacing {
width:100%;
height: 1280px;
position:relative;
}
.counter {text-align:center}
.counter-value {display:inline-block; padding:20px 40px; margin:0 20px; border:1px solid #ddd; font-family:Arial; font-size:50px; font-weight:bold}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="spacing"></div>
<div id="counter1">
<div class="counter-value" data-count="300">0</div>
<div class="counter-value" data-count="400">100</div>
<div class="counter-value" data-count="1500">200</div>
</div>
<div class="spacing"></div>

<div id="counter2">
<div class="counter-value" data-count="500">100</div>
<div class="counter-value" data-count="600">200</div>
<div class="counter-value" data-count="1700">300</div>
</div>
<div class="spacing"></div>





Here's a working codepen - hope that helps!






share|improve this answer























  • @User123 you're welcome :)
    – Dacre Denny
    Nov 15 '18 at 2:13
















1














Perhaps you could revise your implementation by first iterating over each counter using $('#counter1, #counter2').each( .. ).



Inside of each iteration, you'd effectively re-use your existing code by setting up a window.scroll() handler for that counter instance.



You'd also move var a = 0 inside of the iteration, so that your code tracks the unique scroll offset for this counter instance.



Finally, you'd want to ensure that you select '.counter-value' elements for the current counter instance of the iteration:






$('#counter1, #counter2').each(function() {

var a = 0;
var counter = $(this);

$(window).scroll(function() {

var oTop = counter.offset().top - window.innerHeight;

if (a == 0 && $(window).scrollTop() > oTop) {

$('.counter-value', counter).each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({
countNum: $this.text()
}).animate({
countNum: countTo
},

{

duration: 2000,
easing: 'swing',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}

});
});

a = 1;
}

});

})

.spacing {
width:100%;
height: 1280px;
position:relative;
}
.counter {text-align:center}
.counter-value {display:inline-block; padding:20px 40px; margin:0 20px; border:1px solid #ddd; font-family:Arial; font-size:50px; font-weight:bold}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="spacing"></div>
<div id="counter1">
<div class="counter-value" data-count="300">0</div>
<div class="counter-value" data-count="400">100</div>
<div class="counter-value" data-count="1500">200</div>
</div>
<div class="spacing"></div>

<div id="counter2">
<div class="counter-value" data-count="500">100</div>
<div class="counter-value" data-count="600">200</div>
<div class="counter-value" data-count="1700">300</div>
</div>
<div class="spacing"></div>





Here's a working codepen - hope that helps!






share|improve this answer























  • @User123 you're welcome :)
    – Dacre Denny
    Nov 15 '18 at 2:13














1












1








1






Perhaps you could revise your implementation by first iterating over each counter using $('#counter1, #counter2').each( .. ).



Inside of each iteration, you'd effectively re-use your existing code by setting up a window.scroll() handler for that counter instance.



You'd also move var a = 0 inside of the iteration, so that your code tracks the unique scroll offset for this counter instance.



Finally, you'd want to ensure that you select '.counter-value' elements for the current counter instance of the iteration:






$('#counter1, #counter2').each(function() {

var a = 0;
var counter = $(this);

$(window).scroll(function() {

var oTop = counter.offset().top - window.innerHeight;

if (a == 0 && $(window).scrollTop() > oTop) {

$('.counter-value', counter).each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({
countNum: $this.text()
}).animate({
countNum: countTo
},

{

duration: 2000,
easing: 'swing',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}

});
});

a = 1;
}

});

})

.spacing {
width:100%;
height: 1280px;
position:relative;
}
.counter {text-align:center}
.counter-value {display:inline-block; padding:20px 40px; margin:0 20px; border:1px solid #ddd; font-family:Arial; font-size:50px; font-weight:bold}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="spacing"></div>
<div id="counter1">
<div class="counter-value" data-count="300">0</div>
<div class="counter-value" data-count="400">100</div>
<div class="counter-value" data-count="1500">200</div>
</div>
<div class="spacing"></div>

<div id="counter2">
<div class="counter-value" data-count="500">100</div>
<div class="counter-value" data-count="600">200</div>
<div class="counter-value" data-count="1700">300</div>
</div>
<div class="spacing"></div>





Here's a working codepen - hope that helps!






share|improve this answer














Perhaps you could revise your implementation by first iterating over each counter using $('#counter1, #counter2').each( .. ).



Inside of each iteration, you'd effectively re-use your existing code by setting up a window.scroll() handler for that counter instance.



You'd also move var a = 0 inside of the iteration, so that your code tracks the unique scroll offset for this counter instance.



Finally, you'd want to ensure that you select '.counter-value' elements for the current counter instance of the iteration:






$('#counter1, #counter2').each(function() {

var a = 0;
var counter = $(this);

$(window).scroll(function() {

var oTop = counter.offset().top - window.innerHeight;

if (a == 0 && $(window).scrollTop() > oTop) {

$('.counter-value', counter).each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({
countNum: $this.text()
}).animate({
countNum: countTo
},

{

duration: 2000,
easing: 'swing',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}

});
});

a = 1;
}

});

})

.spacing {
width:100%;
height: 1280px;
position:relative;
}
.counter {text-align:center}
.counter-value {display:inline-block; padding:20px 40px; margin:0 20px; border:1px solid #ddd; font-family:Arial; font-size:50px; font-weight:bold}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="spacing"></div>
<div id="counter1">
<div class="counter-value" data-count="300">0</div>
<div class="counter-value" data-count="400">100</div>
<div class="counter-value" data-count="1500">200</div>
</div>
<div class="spacing"></div>

<div id="counter2">
<div class="counter-value" data-count="500">100</div>
<div class="counter-value" data-count="600">200</div>
<div class="counter-value" data-count="1700">300</div>
</div>
<div class="spacing"></div>





Here's a working codepen - hope that helps!






$('#counter1, #counter2').each(function() {

var a = 0;
var counter = $(this);

$(window).scroll(function() {

var oTop = counter.offset().top - window.innerHeight;

if (a == 0 && $(window).scrollTop() > oTop) {

$('.counter-value', counter).each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({
countNum: $this.text()
}).animate({
countNum: countTo
},

{

duration: 2000,
easing: 'swing',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}

});
});

a = 1;
}

});

})

.spacing {
width:100%;
height: 1280px;
position:relative;
}
.counter {text-align:center}
.counter-value {display:inline-block; padding:20px 40px; margin:0 20px; border:1px solid #ddd; font-family:Arial; font-size:50px; font-weight:bold}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="spacing"></div>
<div id="counter1">
<div class="counter-value" data-count="300">0</div>
<div class="counter-value" data-count="400">100</div>
<div class="counter-value" data-count="1500">200</div>
</div>
<div class="spacing"></div>

<div id="counter2">
<div class="counter-value" data-count="500">100</div>
<div class="counter-value" data-count="600">200</div>
<div class="counter-value" data-count="1700">300</div>
</div>
<div class="spacing"></div>





$('#counter1, #counter2').each(function() {

var a = 0;
var counter = $(this);

$(window).scroll(function() {

var oTop = counter.offset().top - window.innerHeight;

if (a == 0 && $(window).scrollTop() > oTop) {

$('.counter-value', counter).each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({
countNum: $this.text()
}).animate({
countNum: countTo
},

{

duration: 2000,
easing: 'swing',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}

});
});

a = 1;
}

});

})

.spacing {
width:100%;
height: 1280px;
position:relative;
}
.counter {text-align:center}
.counter-value {display:inline-block; padding:20px 40px; margin:0 20px; border:1px solid #ddd; font-family:Arial; font-size:50px; font-weight:bold}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="spacing"></div>
<div id="counter1">
<div class="counter-value" data-count="300">0</div>
<div class="counter-value" data-count="400">100</div>
<div class="counter-value" data-count="1500">200</div>
</div>
<div class="spacing"></div>

<div id="counter2">
<div class="counter-value" data-count="500">100</div>
<div class="counter-value" data-count="600">200</div>
<div class="counter-value" data-count="1700">300</div>
</div>
<div class="spacing"></div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 2:13

























answered Nov 15 '18 at 2:04









Dacre DennyDacre Denny

10.6k4929




10.6k4929












  • @User123 you're welcome :)
    – Dacre Denny
    Nov 15 '18 at 2:13


















  • @User123 you're welcome :)
    – Dacre Denny
    Nov 15 '18 at 2:13
















@User123 you're welcome :)
– Dacre Denny
Nov 15 '18 at 2:13




@User123 you're welcome :)
– Dacre Denny
Nov 15 '18 at 2:13


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311211%2fmake-function-to-animate-when-in-view-more-flexible%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)