开发者

jQuery slideTo toggle starting faded

开发者 https://www.devze.com 2023-03-24 06:40 出处:网络
I want to use jQuery to toggle the opacity of an element between 0.3 and 1.0 on a button click. The trouble i am having is starting the element off with an opacity of 0.3 and then on the first click,

I want to use jQuery to toggle the opacity of an element between 0.3 and 1.0 on a button click.

The trouble i am having is starting the element off with an opacity of 0.3 and then on the first click, making it fully visible wit开发者_如何学编程h a 1.0 opacity. The code I have tried is below:

$(document).ready(function(){

    //Start faded to 0.3
    $(".fadingElement").fadeTo(0, 0.3);

    //When the trigger is clicked first, fade the relevant item back up to 1.0
    $("div.trigger").toggle(
        function(){ 
            $(this).parent().next().fadeTo('fast', 1.0);
        }, 
        function () {  
            $(this).parent().next().fadeTo('fast', 0.3);
        }
    );
});

When the "div.trigger" is clicked, it does not fade up, when clicked again, it fades a further 0.3! When clicked a third time, it fades to its starting 0.3.

How do i start the element at 0.3, and bring it back up to fully visible (1.0) on the first click? What is going on here?


The problem (based on the code in your fiddle) is that the element you actually want to fade is <div class="student_notified">, but your code is fading the parent of that div rather than the div itself.

You can use the find method to select the correct element within its parent:

$(this).parent().next().find(".student_notified").fadeTo('fast', 0.3);

Here's an updated fiddle.


You could simply use .animate() with .css('opacity',value) to achieve that behavior

Example:

$("div.trigger").toggle(
$(this).parent().next();
    $(this).parent().next().animate({opacity:1.0});
}, 
function () {  
    $(this).parent().next().animate({opacity:0.3});
   });
}
);
0

精彩评论

暂无评论...
验证码 换一张
取 消