在Javascript中更改元素的显示属性时,如何使元素淡入淡出?

在Javascript中更改元素的显示属性时,如何使元素淡入淡出?,javascript,jquery,css,Javascript,Jquery,Css,单击一个按钮(使用onclick),我将在none和block之间切换三个div的显示样式。我不知道怎样才能使这些div淡入淡出。我也愿意使用JQuery,但我不知道如何在这里集成和使用fadeIn()和fadeOut()函数。任何关于我如何做这件事的建议都将不胜感激 下面是调用的函数onclick以供参考,这是切换my divs的display属性的地方 function divSelector(count){ if (count == 1){

单击一个按钮(使用onclick),我将在
none
block
之间切换三个div的显示样式。我不知道怎样才能使这些div淡入淡出。我也愿意使用JQuery,但我不知道如何在这里集成和使用
fadeIn()
fadeOut()
函数。任何关于我如何做这件事的建议都将不胜感激

下面是调用的函数
onclick
以供参考,这是切换my divs的display属性的地方

function divSelector(count){
            if (count == 1){
                var temp = document.getElementById('second-about-text');
                temp.style.display = 'none';

                temp = document.getElementById('third-about-text');
                temp.style.display = 'none';

                temp = document.getElementById('first-about-text');
                temp.style.display = 'block';
            }
            else if (count == 2){
                var temp = document.getElementById('first-about-text');
                temp.style.display = 'none';
                temp = document.getElementById('third-about-text');
                temp.style.display = 'none';

                temp = document.getElementById('second-about-text');
                temp.style.display = 'block';   
            }
            else if (count == 3){
                var temp = document.getElementById('first-about-text');
                temp.style.display = 'none';
                temp = document.getElementById('second-about-text');
                temp.style.display = 'none';

                temp = document.getElementById('third-about-text');
                temp.style.display = 'block';   
            }   
            else{
                console.log("Count is nothing.");
            }
        }
这是你需要的吗

function divSelector(count){
                if (count == 1){
                    $('#second-about-text').fadeOut();
                    $('#third-about-text').fadeOut();
                    $('#first-about-text').fadeIn();
                }
                else if (count == 2){
                    $('#second-about-text').fadeIn();
                    $('#third-about-text').fadeOut();
                    $('#first-about-text').fadeOut();  
                }
                else if (count == 3){
                    $('#second-about-text').fadeOut();
                    $('#third-about-text').fadeIn();
                    $('#first-about-text').fadeOut();   
                }   
                else{
                    console.log("Count is nothing.");
                }
            }
这是你需要的吗

function divSelector(count){
                if (count == 1){
                    $('#second-about-text').fadeOut();
                    $('#third-about-text').fadeOut();
                    $('#first-about-text').fadeIn();
                }
                else if (count == 2){
                    $('#second-about-text').fadeIn();
                    $('#third-about-text').fadeOut();
                    $('#first-about-text').fadeOut();  
                }
                else if (count == 3){
                    $('#second-about-text').fadeOut();
                    $('#third-about-text').fadeIn();
                    $('#first-about-text').fadeOut();   
                }   
                else{
                    console.log("Count is nothing.");
                }
            }

由于您对JQuery开放,请尝试以下方法:

我只是假设你在这里加价。给你的div指定一个公共类,比如
content
,这样就可以通过一个选择器访问它们

 $('.content:gt(0)').hide(); //start up hide all but the first one.

function divSelector(count){

       var currElem = $('.content:eq(' + count-1 + ')'); //get the div based on the count number eq will give the div based on its index and position in DOM. Assuming these divs appear in DOM one after the other.
        $('.content').not(currElem).fadeOut('1000', function(){ //Fade out other content divs
             currElem.fadeIn('1000'); //Fade in the current one.
        });

    }

提供您的标记将消除这些假设,并帮助提供更好的解决方案。

既然您对JQuery持开放态度,请尝试以下方法:

我只是假设你在这里加价。给你的div指定一个公共类,比如
content
,这样就可以通过一个选择器访问它们

 $('.content:gt(0)').hide(); //start up hide all but the first one.

function divSelector(count){

       var currElem = $('.content:eq(' + count-1 + ')'); //get the div based on the count number eq will give the div based on its index and position in DOM. Assuming these divs appear in DOM one after the other.
        $('.content').not(currElem).fadeOut('1000', function(){ //Fade out other content divs
             currElem.fadeIn('1000'); //Fade in the current one.
        });

    }

提供您的标记将清除假设,并帮助提供更好的解决方案。

以下是一个完整的示例:


.btn{高度:50px;宽度:50px;边距:50px 50px;}
.1{背景色:红色;}
.two{背景色:绿色;}
.3{背景色:蓝色;}
$(文档).ready(函数(){
计数=1;
$('#mybutt')。单击(函数(){
除法选择器(计数);
计数++;
如果(计数=4)计数=1;
});
函数选择器(计数){
如果(计数=1){
$(“#首先关于文本”).fadeIn(1000);
$(“#第二个关于文本”).hide();
$(“#第三个关于文本”).fadeOut(1000);;
}否则如果(计数=2){
$(“#首先关于文本”).fadeOut(1000);
$(“#关于文本的第二次”)。fadeIn(1000);
$(“#第三个关于文本”).hide();;
}否则如果(计数=3){
$(“#首先关于文本”).hide();
$(“#第二个关于文本”).fadeOut(1000);
$(“#关于文本的第三次”).fadeIn(1000);;
}否则{
log(“计数什么都不是”);
}
}
}); //END$(document.ready())

以下是一个完整的示例:


.btn{高度:50px;宽度:50px;边距:50px 50px;}
.1{背景色:红色;}
.two{背景色:绿色;}
.3{背景色:蓝色;}
$(文档).ready(函数(){
计数=1;
$('#mybutt')。单击(函数(){
除法选择器(计数);
计数++;
如果(计数=4)计数=1;
});
函数选择器(计数){
如果(计数=1){
$(“#首先关于文本”).fadeIn(1000);
$(“#第二个关于文本”).hide();
$(“#第三个关于文本”).fadeOut(1000);;
}否则如果(计数=2){
$(“#首先关于文本”).fadeOut(1000);
$(“#关于文本的第二次”)。fadeIn(1000);
$(“#第三个关于文本”).hide();;
}否则如果(计数=3){
$(“#首先关于文本”).hide();
$(“#第二个关于文本”).fadeOut(1000);
$(“#关于文本的第三次”).fadeIn(1000);;
}否则{
log(“计数什么都不是”);
}
}
}); //END$(document.ready())