简体   繁体   English

如何在此 javascript 代码中添加淡入淡出效果?

[英]How do I add a fadeIn effect in this javascript code?

I got the following javascript code:我得到以下 javascript 代码:

function myProfileIn() {
    var showme = document.getElementById("myProfileTop").fadeIn(2000);
    showme.style.display = "inline";
    var showme = document.getElementById("myProfileMain").fadeIn(2000);
    showme.style.display = "inline";
}

This code works on my buttons as "onclick="myProfileIn();" so they get to be visible but i also want to include a fadein effect when the button is clicked. That is why i included ".fadeIn(1000)" but this is not working.这段代码在我的按钮上作为"onclick="myProfileIn();"所以它们是可见的,但我也想在单击按钮时包含淡入效果。这就是为什么我包含".fadeIn(1000)"但是这是行不通的。

Here is the button which activates the function:这是激活该功能的按钮:

<div id="profileButtonArea"><div id="profileButtonBox"><img onclick="myProfileIn();" id="profileButtonImg" src="<?php echo $userPath; ?>" /></div></div>

And here are the two elements(divs) which are blended in:这是混合的两个元素(div):

<div id="myProfileTop" style="display:none;"></div>
<div id="myProfileMain" style="display:none;"></div>

The issue you have is that fadeIn() is a jQuery method.您遇到的问题是fadeIn()是一种jQuery 方法。 It's not available on a standard HTMLElement object.它在标准 HTMLElement 对象上不可用。 Also note that if you want to toggle the element's visibility on successive clicks use fadeToggle() .另请注意,如果您想在连续单击时切换元素的可见性,请使用fadeToggle()

You should also use unobtrusive Javascript to attach your event handlers as on* event attributes are now considered outdated.您还应该使用不显眼的 Javascript 来附加您的事件处理程序,因为on*事件属性现在被认为是过时的。 As you've tagged the question with jQuery.正如您使用 jQuery 标记问题一样。 Here's a working example for all of the above:这是上述所有内容的工作示例:

 $(function() { $('#profileButtonBox img').click(function() { $("#myProfileTop, #myProfileMain").stop(true).fadeToggle(2000); }); });
 #myProfileTop, #myProfileMain { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="profileButtonArea"> <div id="profileButtonBox"> <img id="profileButtonImg" src="yourimage.jpg" title="click me" /> </div> </div> <div id="myProfileTop">myProfileTop</div> <div id="myProfileMain">myPropfileMain</div>

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

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