gpt4 book ai didi

javascript - 使用jquery在单击时淡入正文背景

转载 作者:行者123 更新时间:2023-11-29 18:25:52 27 4
gpt4 key购买 nike

现在,我正在使用 switchClass 来更改正文背景。这种变化是即时的,而且对眼睛来说并不容易。

$('.Click').click(function () {
var thisClass = $(this).attr('class');
var st = thisClass.split(' ');
var firstClass = st[0];

if (firstClass == "item1") {
$('.mask').animate({
marginLeft: "-100%"
}, 600, function () {
$("body").switchClass($("body").attr("className"), "bg1");
});
} else if (firstClass == "item2") {
$('.jobsMask').animate({
marginLeft: "-200%"
}, 600, function () {
$("body").switchClass($("body").attr("className"), "bg2");
});
}
});

我如何制作动画/让它不那么突兀?

最佳答案

对于 background-color 褪色:

您可以在 body 元素上使用过渡来为 background 属性的更改设置动画,如下所示:

body { 
-ms-transition: background 0.8s ease 0s;
-moz-transition: background 0.8s ease 0s;
-o-transition: background 0.8s ease 0s;
-webkit-transition: background 0.8s ease 0s;
transition: background 0.8s ease 0s;
}

测试它:http://jsbin.com/odabit/2/edit


对于淡化背景图像:

Transitions 似乎不适用于 background-image(编辑:它们在 Chrome 中有效,但在 Fx16、Opera12.1 或 IE9 中无效),所以我的解决方法是将您的 bg单独的 div 上的图像,然后将它们淡入淡出,如下所示:http://jsbin.com/odabit/7/edit

切换代码如下所示:

var cls1 = 'bg1', cls2 = 'bg2',
body = $('body'),
bg1 = body.hasClass(cls1) || !!body.children('.'+cls1)[0],
bg = bg1 ? body.children('.'+cls1) : body.children('.'+cls2),
newBg;

if (!bg[0]) {
bg = $('<div>', {'class': (bg1 ? cls1 : cls2)}).prependTo(body);
}

newBg = $('<div>', {'class': (bg1 ? cls2 : cls1)}).insertAfter(bg).hide();

body.removeClass(cls1 +' ' +cls2);

bg.fadeOut('slow', function() { bg.remove(); });
newBg.fadeIn('slow');

以及支持它的 CSS:

body > div[class^="bg"] {
position: fixed;
top: 0; right: 0; bottom: 0; left: 0;
z-index: -1;
}

.bg1 {
background: url(/* image 1 */);
}

.bg2 {
background: url(/* image 2 */);
}

关于javascript - 使用jquery在单击时淡入正文背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13276770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com