Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery Won't run

Posted on 2012-03-28
8
Medium Priority
?
311 Views
Last Modified: 2012-03-29
Hey Experts,

I'm trying to get my first jQuery to run. I have attached a zip file of the problem code and a summary is below:

I have this in the HTML file:

...
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascript/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="javascript/functions.js"></script>
</head>
<body onload="animateMLAR();">
...

I have this in the functions.js file:

$(document).ready(function(){
      function animateMLAR(){
            $('#Mamma').animate({backgroundPosition: '(-500px -80px)'}, 20000);
      }
}

And these are the properties in the CSS file:

#Mamma {
background: url(../images/ML.png) no-repeat center top;
width: 450px;
height: 583px;
float: right;
margin-right: 60px;
margin-top: -20px;
}


Any help would be great!
Basic-Site.zip
0
Comment
Question by:jeffiepoo
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37779746
change this:
<body onload="animateMLAR();">

to this:
<body>

and this:
$(document).ready(function(){
      function animateMLAR(){
            $('#Mamma').animate({backgroundPosition: '(-500px -80px)'}, 20000);
      }
}

Open in new window

to this:
$(document).ready(function(){
      function animateMLAR(){
            $('#Mamma').animate({backgroundPosition: '(-500px -80px)'}, 20000);
      }
      animateMLAR();
}

Open in new window

0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37779751
PS: the thing is, "body onLoad" is run when the document loads.
However, the DOM isn't yet fully "grown up" inside the browser.
That's why jQuery provides $(document).ready
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 37779997
It sounded promising, but it still didn't do anything. Any further suggestions?

Here is the updated package.

Thanks in advance.
Basic-Site.zip
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37780476
check your syntax... you are lacking a closing bracket in functions.js
0
 
LVL 16

Accepted Solution

by:
Steve Krile earned 2000 total points
ID: 37781323
OK, found several issues with your approach.  

The most important - you need to include the Background Position plugin in your project.  Moving the background is not a built-in feature of jquery.

Second, there were a bunch of little typos in your jquery.

Finally, the function to run the animation is best left *outside* of the $(document).ready() section.

So, the head of your HTML page would look like this:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Mamma Lenny and the Remedy Official Site</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascript/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://www.protofunc.com/scripts/jquery/backgroundPosition/jquery.backgroundPosition.js"></script>
<script type="text/javascript" src="javascript/functions.js"></script>
</head>

Open in new window


And your jquery section like this:
$(document).ready(function () {
    animateMLAR(); 
});

function animateMLAR(){
    $("#Mamma").animate({ backgroundPosition: "-500px -80px" }, 2000);

}

Open in new window

0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 37785229
Wow that got it working! I made the same changes in my MVC version of the site and nothing is working again it is driving me crazy! Any ideas? How do you troubleshoot this stuff?

You need Visual Studio with MVC to see the solution. It is double zipped because Experts Exchange is dumb like that.
Basic-Site-in-MVC.zip
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 37785249
Using this:

$(document).ready(function () {
    animateMLAR();
});

function animateMLAR() {
    $("#Mamma").animate({ backgroundPosition: "-500px -80px" }, 2000);
}
                         

It says in Firebug that I have 'missing ) after argument list'
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37785279
try this:

function animateMLAR() {
    $("#Mamma").animate({ 'background-position-x': '-500', 'background-position-y': '-80' }, 2000);
}
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question