【问题标题】:Why is fadeIn flickering when loading?为什么fadeIn在加载时闪烁?
【发布时间】:2013-01-18 18:35:42
【问题描述】:

我根据 youtube 教程制作了一个网站。 Here's my test site。和 here's the tutorial (final step of 3) 如果有帮助。尚未使用 CSS。

当我单击菜单中的链接时。一切都像想象的那样消失。但在淡入淡出动画结束时上一页闪烁。

我
我对 JavaScript 完全陌生。

index.php:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SuperFresh</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<a class="menu_top" href="pages/home.php">Home</a> /
<a class="menu_top" href="pages/portfolio.php">Portfolio</a> /
<a class="menu_top" href="pages/contact.php">Contact</a> /

<div id="content_area"></div>

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="js/nav.js"></script>
</body>
</html>

nav.js:

$(document).ready(function () {
    $('#content_area').load($('.menu_top:first').attr('href'));
});

$('.menu_top').click(function () {
    var href=$(this).attr('href');
    $('#content_area').hide('slow').load(href).fadeIn('slow');

    return false;
});

【问题讨论】:

  • 你是对的。但我无法让 index.php 工作。 =/ 所以我提交了我的测试站点 ...

标签: php jquery menu fadein flicker


【解决方案1】:

替换:

var href=$(this).attr('href');    
$('#content_area').hide('slow').load(href).fadeIn('slow');

与:

$('#content_area').hide('slow', function(){
    $(this).load($(this).attr('href')).fadeIn('slow');
});

这将使其淡出并等待动画完成,然后再调用该函数并加载新内容。

查看the documentation 了解更多信息。

【讨论】:

  • 没有闪烁,但 href 只是显示 home.php,尽管我单击了哪个链接。看看test site。
【解决方案2】:

您可以将处理程序与您自己的函数一起使用...

例如:

$('#show').click(function() {
    $('#myDiv').show(0, onDivShow);
});

$('#hide').click(function() {
    $('#myDiv').hide(0, onDivHide);
});


function onDivShow() { alert('is shown'); }
function onDivHide() { alert('is hidden'); }

【讨论】:

  • 我对 javascrip 知之甚少,所以无法实现您的代码。太糟糕了,它看起来很结实。
【解决方案3】:

你可以试试这个:

$('.menu_top').click(function () {
    var href=$(this).attr('href');
    var page = $('<div/>').attr('id','page').load(href);
    $('#content_area').fadeOut('slow').html('').html(page).fadeIn('slow');

    return false;
});

【讨论】:

  • Here it is with your code Jai。它现在消失了两次。但这不像以前那么烦人了。你可能知道为什么会出现两次淡入淡出吗?
  • 我已经用你的更新更新了the test site。但仍然有双重淡入淡出。你比我厉害,所以我看不到你的更新做了什么。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2013-05-26
  • 2011-10-01
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
相关资源
最近更新 更多