javascript - 单击其他列表项时淡出

标签 javascript jquery html css fadeout

本周早些时候,我使用了一个教程来实现类似的功能,但我决定将其简化为基本功能。我希望用户能够单击他们想了解的瓶子,然后打开一个内容。但是如果一个人没有点击 X 而是点击其他瓶子,它应该淡出当前的内容然后显示新点击的那个。

我对 J Query 的了解有限,所以我知道我正在使用一种更长的技术,通过它们的 ID 来定位每个查询。如果有人可以给我看,我会喜欢一个结构更短的版本。

但根据我的知识,这是我可以构造的。

HTML

    <div id="two" class="colorbottle">
    <img src="images/2.png">
    </div>



    <div id="three" class="colorbottle">
    <img src="images/3.png">
    </div>



    <div id="four" class="colorbottle">
    <img src="images/4.png">
    </div>

    <div class="clearer"></div>
    </div>


    <div class="ei_descr" id="onedescr">
    <div class="contact_close">X</div>

        <h2>Gary</h2>
        <h3>Vocals</h3>
        <p>
        A wonderful serenity has taken possession of my
        entire soul, like these sweet mornings of
        spring which I enjoy with my whole heart.
        </p>
        <p>
        I am alone, and feel the charm of existence in
        this spot, which was created for the bliss of
        souls like mine.
        </p>
    </div>

    <div class="ei_descr" id="twodescr">
    <div class="contact_close">X</div>

        <h2>Gary</h2>
        <h3>Vocals</h3>
        <p>
        A wonderful serenity has taken possession of my
        entire soul, like these sweet mornings of
        spring which I enjoy with my whole heart.
        </p>
        <p>
        I am alone, and feel the charm of existence in
        this spot, which was created for the bliss of
        souls like mine.
        </p>
    </div>


    <div class="ei_descr" id="threedescr">
    <div class="contact_close">X</div>
        <h2>Gary</h2>
        <h3>Vocals</h3>
        <p>
        A wonderful serenity has taken possession of my
        entire soul, like these sweet mornings of
        spring which I enjoy with my whole heart.
        </p>
        <p>
        I am alone, and feel the charm of existence in
        this spot, which was created for the bliss of
        souls like mine.
        </p>
    </div>


    <div class="ei_descr" id="fourdescr">
    <div class="contact_close">X</div>

        <h2>Gary</h2>
        <h3>Vocals</h3>
        <p>
        A wonderful serenity has taken possession of my
        entire soul, like these sweet mornings of
        spring which I enjoy with my whole heart.
        </p>
        <p>
        I am alone, and feel the charm of existence in
        this spot, which was created for the bliss of
        souls like mine.
        </p>
    </div>

Javascript

$(document).ready(function() {


   $(".colorbottle").click(function () {
    if(!$(this).hasClass('selected'))
    {
        $(".colorbottle.selected").removeClass("selected");
        $(this).addClass("selected");        
    }
});


$("#one").click(function() {

    $("#onedescr").fadeIn(1000);
    $(".colorbottle").not( ".selected").animate({opacity:0.2});
    $(".selected").animate({opacity:1});

})

$(".contact_close").click(function() { 
$(".ei_descr").fadeOut("slow"); 
$(".colorbottle").animate({opacity:1});

 });


$("#two").click(function() {
    $("#twodescr").fadeIn(1000);
    $(".colorbottle").not( ".selected").animate({opacity:0.2});
    $(".selected").animate({opacity:1});

})

   $("#three").click(function() {
    $("#threedescr").fadeIn(1000);
    $(".colorbottle").not( ".selected").animate({opacity:0.2});
            $(".selected").animate({opacity:1});

})

$("#four").click(function() {
    $("#fourdescr").fadeIn(1000);
    $(".colorbottle").not( ".selected").animate({opacity:0.2});
            $(".selected").animate({opacity:1});

})

});

现在它所做的只是淡入被点击的瓶子,并使其他瓶子透明。 这是 fiddle

谢谢

最佳答案

你需要使用html data- attribute来存储点击瓶子div时需要显示的div的id:

HTML:

<div id="wrapcraft">
    <div id="one" class="colorbottle" data-id="onedescr">
        <img src="http://i61.tinypic.com/v8n2hv.png">
    </div>
    <div id="two" class="colorbottle" data-id="twodescr">
        <img src="http://i61.tinypic.com/v8n2hv.png">
    </div>
    <div id="three" class="colorbottle" data-id="threedescr">
        <img src="http://i61.tinypic.com/v8n2hv.png">
    </div>
    <div id="four" class="colorbottle" data-id="fourdescr">
        <img src="http://i61.tinypic.com/v8n2hv.png">
    </div>
    <div class="clearer"></div>
</div>
<div class="ei_descr" id="onedescr">
    <div class="contact_close">X</div>
     <h2>Gary</h2>

     <h3>Vocals</h3>

    <p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart.</p>
    <p>I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.</p>
</div>
<div class="ei_descr" id="twodescr">
    <div class="contact_close">X</div>
     <h2>Gary</h2>

     <h3>Vocals</h3>

    <p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart.</p>
    <p>I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.</p>
</div>
<div class="ei_descr" id="threedescr">
    <div class="contact_close">X</div>
     <h2>Gary</h2>

     <h3>Vocals</h3>

    <p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart.</p>
    <p>I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.</p>
</div>
<div class="ei_descr" id="fourdescr">
    <div class="contact_close">X</div>
     <h2>Gary</h2>

     <h3>Vocals</h3>

    <p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart.</p>
    <p>I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.</p>
</div>

JQUERY:

 $(document).ready(function () {

     $(".colorbottle").click(function () {
         if (!$(this).hasClass('selected')) {
             $(".colorbottle.selected").removeClass("selected");
             $(this).addClass("selected");
             $(".ei_descr").fadeOut(1000);
             $('#' + $(this).data("id")).fadeIn(1000);
             $(".colorbottle").not(".selected").animate({
                 opacity: 0.2
             });
             $(".selected").animate({
                 opacity: 1
             });
         }
     });

 });

UPDATED DEMO

关于javascript - 单击其他列表项时淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26741684/

相关文章:

javascript - vue dev工具中的警告

JavaScript 获取传入参数的函数实例

javascript - 按照教程时 react 错误

javascript - 具有动态加载内容的可折叠列表

javascript - 在不使用 jQuery UI 的情况下使用 jQuery 模拟 div 的帧调整大小行为?

html - 如何保持事件链接的颜色不变,直到我按下其他链接

javascript - 当用户更改输入时,React 不会更新组件状态

jquery,id中类的选择器

javascript - 表单选择下拉选项不在正确的位置

javascript - jQuery 多重显示隐藏在一种形式中