开发者

jQuery FadeIn/Out Attributes

开发者 https://www.devze.com 2023-02-12 17:11 出处:网络
I would like to be able to modify the code below to fadeOut and then FadeIn the removal and adding of the style attribute from body_id.

I would like to be able to modify the code below to fadeOut and then FadeIn the removal and adding of the style attribute from body_id.

I know I can fade the whole of body_id but I would like to anim开发者_StackOverflow中文版ate the removal and adding of the style attribute only not everything inside the id itself?

$(".bg").click(function(){
var1 = $(this).attr('id');
loc = "background-image:url(" + var1 + ")"

$("#body_id").removeAttr("style").attr("style", loc + ";");
$.cookie('pageBG', loc,{ expires: 7, path: '/'});
return false;
});


Not really possible in this case, since we're talking about a background-image here, and there's no way to change the opacity of a background-image through CSS.

You can either change the master opacity (.fadeIn/.fadeOut) of the element or use a separate element positioned in the same position as the background and fade that instead.


Separate cleanup of your current code:

$(".bg").click(function(){
    var bgUrl = this.id;

    $("#body_id").css('backgroundImage', bgUrl);
    $.cookie('pageBG', bgUrl, { expires: 7, path: '/'});

    return false;
});

Remember to change the part of your code that reads out the cookie too.


var $body_id = $('#body_id');

$body_id.fadeOut(function() {

    // This runs when the fadeOut is complete.
    $body_id.removeAttr('style').fadeIn();

});
0

精彩评论

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