dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Script does not work externally

  1. #1
    Join Date
    Mar 2014
    Posts
    3

    resolved [RESOLVED] Script does not work externally

    I have a script that works great and does exactly what I need it to, when I call it internally. However, if I add it as an external script, it does not run. I'm pulling hairs because I cannot figure out why. An extra pair of eyes would be awesome. There's nothing wrong with the way I call it, externally, either.

    HTML Code:
    <script src="parallax.js" type="text/javascript"></script>
    Code:
    var bigYellowCircle = document.querySelector("#bigYellowCircle");
    var blueSquare = document.querySelector("#blueSquare");
    var greenPentagon = document.querySelector("#greenPentagon");
     
    function setTranslate(xPos, yPos, el) {
    	el.style.transform = "translate3d(" + xPos + ", " + yPos + "px, 0)";
    }
     
    window.addEventListener("DOMContentLoaded", scrollLoop, false);
     
    var xScrollPosition;
    var yScrollPosition;
     
    function scrollLoop() {
    	xScrollPosition = window.scrollX;
    	yScrollPosition = window.scrollY;
     
    	setTranslate(0, yScrollPosition * -0.2, bigYellowCircle);
    	setTranslate(0, yScrollPosition * -1.5, blueSquare);
    	setTranslate(0, yScrollPosition * .5, greenPentagon);
     
    	requestAnimationFrame(scrollLoop);
    }

  2. #2
    Join Date
    Mar 2014
    Posts
    3
    Nevermind, I understand where I'm messing up. I am putting the external code in my header, which is creating an issue. The script is running and trying to interact with data that is not yet present, as far as the script is concerned. I put it at the bottom of my body and everything is working correctly. Derp moment, for real. Sorry for the unnecessary thread.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,993
    You can use the onload event to run your script when the page has loaded, doing what you have done tells me that your script is running as soon as it is encountered, if loading the script, you can defer its loading with the defer attribute.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles