简体   繁体   English

如何使用JavaScript向HTML过渡添加淡入淡出效果

[英]How to add a fade effect to html transition with javascript

I have this code (see jsfiddle below) 我有此代码(请参见下面的jsfiddle)

<script type='text/javascript'> https://jsfiddle.net/mA8hj/ and would like to know how to edit the javascript in a way that by clicking the links in the fiddle, the text displayed fades slowly to another text when clicking another link. <script type='text/javascript'> https://jsfiddle.net/mA8hj/,并想知道如何编辑Javascript,方法是单击小提琴中的链接,显示的文本逐渐淡入另一个文本单击另一个链接时。 (Something like you'd use in CSS by adding -ms-transition: .2s;). (就像您在CSS中使用-ms-transition:.2s;一样)。

Thanks! 谢谢!

Try to pass "slow"/"fast"/"medium"/any delay in milliseconds as a parameter to show , 尝试将"slow"/"fast"/"medium"/any delay in milliseconds"slow"/"fast"/"medium"/any delay in milliseconds作为参数show

$('[class^="question"]').on('click', function(e){
    e.preventDefault();
    var numb = this.className.replace('question', '');
    $('[id^="answer"]').hide();
    $('#answer' + numb).stop().show("slow");
});

DEMO 演示

Or by using .fadeIn() 或使用.fadeIn()

$('[class^="question"]').on('click', function(e){
    e.preventDefault();
    var numb = this.className.replace('question', '');
    $('[id^="answer"]').hide();
    $('#answer' + numb).css("opacity",0).stop().fadeIn("slow");
});

Use a CSS class and CSS transitions instead, and use jQuery only to add/remove this class .show : 请改用CSS类和CSS过渡,并仅使用jQuery添加/删除该类.show

 $('[class^="question"]').on('click', function(e){ e.preventDefault(); var numb = this.className.replace('question', ''); $('[id^="answer"]').removeClass("show"); $('#answer' + numb).addClass("show"); }); 
 #answer, #answer1, #answer2, #answer3, #answer4 { position: absolute; opacity: 0; transition-duration: 0.4s; } #answer.show, #answer1.show, #answer2.show, #answer3.show, #answer4.show { position: absolute; opacity: 1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="new_member_box"> <a href="#" class="question1"><h4>Vision</h4></a> </div> <div class="new_member_box"> <a href="#" class="question2"><h4>Church History</h4></a> </div> <div class="new_member_box"> <a href="#" class="question3"><h4>Becoming a Member</h4></a> </div> <div class="new_member_box"> <a href="#" class="question4"><h4>Pastor-in-Training</h4></a> </div> <div class="clear" class="question"></div> <div id="answer1">1</div> <div id="answer2">2</div> <div id="answer3">3</div> <div id="answer4">4</div> <div class="new_member_box_display show" id="answer">Text will appear here when one of the tabs above is clicked</div> 

https://jsfiddle.net/mA8hj/94/ https://jsfiddle.net/mA8hj/94/

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

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