Reputation: 2533
I'd like to add a simple counter to my webpage using countUp.js
.
Here is my code:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N"
crossorigin="anonymous">
<title>Test</title>
</head>
<body>
<div class="container-fluid">
<div class="row justify-content-center">
<div id="test-counter" class="col-8 border">
0
</div>
</div>
</div>
<!-- - - - - - - - - - - - - - - - - - - >
< Load the JavaScript Libraries >
<- - - - - - - - - - - - - - - - - - - -->
<!-- own JavaScript -->
<script src="js/countUp.js"></script>
<!-- CountUp.js library -->
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/countUp.js"
></script>
</body>
</html>
js/countUp.js file
const c = new countUp.CountUp('test-counter', 100)
c.start()
I tried to implement this according to the advice given on CountUp is not defined, but it's not working.
Questions:
Is my file structure correct? (i.e. do I instantiate an object c
and call it from within a separate countUp.js
file (which itself is imported into the index.html
file?
Am I importing countUp.js
correctly from its source? (I'm new to JavaScript)
Could I just take the contents of https://github.com/inorganik/countUp.js/blob/master/dist/countUp.js and drop that into a file in my project? (then reference this file locally)
Thanks in advance for any help you can give!
Upvotes: 0
Views: 1698
Reputation: 106
As mentioned in the comments you cant use anything from a script that was not yet parsed (the browser parse the html file from top to bottom line after line with some exceptions).
new
key word on countUp instead of CountUp.Upvotes: 1