简体   繁体   中英

Why isn't my web page transition working? (HTML, CSS, JavaScript)

Here is my HTML:

<!DOCTYPE html>
<html>
<head>
<link href="index.css" rel="stylesheet" type="text/css">
<script src="main.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>

<body>
<a id="myLink" href="#">
            Click to slide in new page
    </a>


<iframe id="newPage" src="http://jsfiddle.net"></iframe>
</body> 
</html>

And here is my CSS:

 #myLink {
 position: absolute;
}

iframe {
width: 100%;
height: 100%;
top: 100%;    
 position: fixed;
 background-color: blue;

}

And my JavaScript:

$("#myLink").click(function () {
$('#newPage').transition({top: '0%' });
});

I am literally copy and pasting from this source http://jsfiddle.net/TheGAFF/hNYMr/ to achieve a transition between web pages using iframe but for some reason when I try this simple code in my browser, it doesn't work. I can't see why this doesn't work in my browser when I link it to index.html. Can anyone help? Thanks.

Edit: The "#myLink" in the CSS page isn't commented out, it just happened to format like this in the question.

Look in your JavaScript console . Expect to see an error about $ not being defined.

See this code:

<script src="main.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

First you load your script, which tries to use $ . Then you try to load jQuery, which defines $ .

Swap the order or your script elements.


You then have a second problem.

$("#myLink")

You have no element with id="myLink" in the document at the time the script runs.

It doesn't get added to the DOM until 4 lines later.

Either:

  • move the script so it appears after the elements you are trying to access
  • use an event handler (like DOM Ready):

Such:

$( function () {
    $("#myLink").click(function () {
        $('#newPage').transition({top: '0%' });
    });
} );
  • use delegated events

Such:

$(document).on("click", "#myLink", function () {
    $('#newPage').transition({top: '0%' });
});

Edit; sorry you already did that.

Try puttting your js file Under the js library file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM