简体   繁体   English

jQuery淡入活跃元素,淡出不活跃元素

[英]jQuery fade in active element, fade out inactive elements

Update 更新

Using: 使用:

$(event.currentTarget).fadeTo(0, 1);

Seems to work, while using: 在使用时似乎可以工作:

$('.btn .active').fadeTo(0, 1);

Does not. 才不是。 Any idea why? 知道为什么吗?

The Code 编码

jsFiddle link here: http://jsfiddle.net/SeanKilleen/fwerK/ 此处的jsFiddle链接: http : //jsfiddle.net/SeanKilleen/fwerK/

JavasScript code below: 下面的JavasScript代码:

var global_loggedOnUser = "User1";

$(document).ready(function () {

    var viewmodel = (function () {
        this.feedbacktype = ko.observable("None");
        this.currentPage = ko.observable(location.href);
        this.currentUsername = global_loggedOnUser;

        this.updateFeedbackType = function (item, event) {
            var newText = $(event.currentTarget).children("span").text();
            $('#buttonList button').removeClass('active');
            $(event.currentTarget).addClass('active');

            feedbacktype(newText);
            $('.btn').not('.active').fadeTo('fast', 0.3);
            $('.btn .active').fadeTo('fast', 1);
        };



        return {
            pageUserIsOn: currentPage,
            theUser: currentUsername,
            feedbackType: feedbacktype
        };
    })();


    ko.applyBindings(viewmodel);
});

The Goal 目标

  • I have a list of buttons. 我有一个按钮列表。
  • When someone clicks on the button, I want to ensure the button they click on becomes 100% opacity, and the rest of the buttons become 30% capacity. 当某人单击按钮时,我要确保他们单击的按钮变为100%不透明,其余按钮变为30%的容量。

I'm trying to accomplish this by adding an "active" class to the button and removing it from all others, and then performing the fade based on class. 我正在尝试通过向按钮添加“活动”类并将其从所有其他按钮中删除,然后基于该类执行淡入淡出来实现此目的。

The Issue 问题

  • The first click, it works as expected. 第一次单击,它可以正常工作。 The clicked button is at 100% opacity and all others fade. 单击的按钮的不透明度为100%,所有其他按钮都消失。
  • The second time, the previously highlighted element fades, but the clicked button doesn't become 100% opacity, despite being given the "active" css class. 第二次,以前突出显示的元素消失了,但是尽管被赋予了“活动的” css类,但单击的按钮仍未达到100%不透明度。
    • I added a border-size element to the active class so I could verify this. 我在活动类中添加了border-size元素,因此可以进行验证。 The clicked item expands its border, but does not fade to 100% opacity. 单击的项目会扩展其边框,但不会淡入100%的不透明度。

What am I missing? 我想念什么?

Remove the space between the classes, you want to select elements with both classes not .active descendants of .btn 删除类之间的空间,您要选择两个类都不是.active后代的.btn

$('.btn .active').fadeTo('fast', 1);

should be 应该

$('.btn.active').fadeTo('fast', 1);

Updated fiddle 更新的小提琴

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

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