Reputation: 153
I need an iframe to fade out the old page and then fade in with the new page when the user clicks one of the various link and I can’t get the code to work. Just wondering if you can see where I went wrong.
<div id="menubar1">
<a href="link1" target="iframe1"> link 1 </a>
<a href="link 2" target="iframe1"> link 2 </a>
<a href="link 3" target="iframe1"> link 3 </a>
<a href="link 4" target="iframe1"> link 4 </a>
</div>
<iframe id="iframe1" name="iframe1" src="homepage.html" frameborder="0"></iframe>
<script>
$("#menubar1 a").click(function() {
$("#iframe1").FadeOut("slow");
$("#iframe1").FadeIn("slow");
});
</script>
Upvotes: 1
Views: 4623
Reputation: 3799
This way should work.
$("#menubar1 a").click(function(e) {
e.preventDefault();
var src = $(this).attr('href');
$('#iframe1').fadeOut(1000,function(){
$('#iframe1').attr('src',src );
$('#iframe1').fadeIn(1000);
});
});
You can now remove the target attribute since we've handled it via jQuery.
<a href="link1"> link 1 </a>
I've tested this on jsfiddle to see how it works. jsFiddle
Then I noticed the iframe
is blinking after the content is fully loaded. So I added a load()
function to fix the blinking and it's working very well. See this jsFiddle
$("#menubar1 a").click(function(e) {
e.preventDefault();
var src = $(this).attr('href');
$('#iframe1').fadeOut(1000,function(){
$('#iframe1').attr('src',src ).load(function(){
$(this).fadeIn(1000);
});
});
});
Upvotes: 5
Reputation: 5197
Simply a syntax error with the capitalization of .fadeOut (you wrote .FadeOut).
$("#menubar1 a").click(function() {
$("#iframe1").fadeOut("slow");
$("#iframe1").fadeIn("slow");
});
Although I recommend you do the following:
<html>
<body>
<div id="menubar1">
<a href="#" id="link1"> link 1 </a>
<a href="#" id="link 2"> link 2 </a>
<a href="#" id="link 3"> link 3 </a>
<a href="#" id="link 4"> link 4 </a>
</div>
<iframe id="iframe1" name="iframe1" src="homepage.html" frameborder="0" >
</iframe>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$("#menubar1 a").click(function() {
var linkId = this.id; // id of clicked a element
$("#iframe1").fadeOut("slow", function() {; // fade out iframe
document.getElementById("iframe1").src = linkId; // on completion of fade out - change iframe src attribute
$("#iframe1").fadeIn("slow"); //fade in iframe
});
});
</script>
</body>
</html>
This way the contents of your iframe will not change before the fadeOut.
Upvotes: -2