简体   繁体   English

jQuery淡入淡出效果替换div

[英]jQuery fade effect on replacing divs

I've been struggling to find a solution for a fade effect with this script. 我一直在努力寻找该脚本的fade效果的解决方案。 I'm still learning basic jQuery, but cant seem to figure it out. 我仍在学习基本的jQuery,但似乎无法弄清楚。 The script is working fine, the divs are replacing, but with no animation. 脚本工作正常,div正在替换,但是没有动画。 I have tried fadeToggle, fadeIn and fadeOut. 我尝试了fadeToggle,fadeIn和fadeOut。

function HideContent(d) {
    if(d.length < 1) {
        return;
    }
    document.getElementById(d).style.display = "none";
}

function ShowContent(d) {
    if(d.length < 1) {
        return;
    }
    document.getElementById(d).style.display = "block";
    $('#' + d).fadeToggle('slow');
}

I call the script with onmouseclick. 我用onmouseclick调用脚本。 I have about 14 different divs. 我大约有14个不同的div。

<a  onmouseclick="ShowContent('div1'); return true; "href="javascript:ShowContent('div1')"> Link button </a>

and to go back: 并返回:

<a onmouseclick="HideContent('div1'); return true;" href="javascript:HideContent('div1')"> BACK </a>

How do I fix this problem? 我该如何解决这个问题?

There are a couple issues in your code, but the one stopping the animation happens because you already changed the display CSS style to block before you call the fadeToggle. 您的代码中有几个问题,但是停止动画的一个问题是发生的,因为您已经在调用fadeToggle之前将显示CSS样式更改为block Try the code below instead. 请尝试以下代码。

function HideContent(d) {
    if(d.length < 1) {
        return; 
    }

    // document.getElementById(d).style.display = "none";
    // replace the above line with:
    // $('#' + d).css('display','none');

    // or even better, fade it out:
    $('#' + d).fadeOut('slow');
}

function ShowContent(d) {
    if(d.length < 1) {
        return; 
    }
    // document.getElementById(d).style.display = "block";
    // You don't need the above, the fadeToggle does this for you.

    $('#' + d).fadeIn('slow');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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