简体   繁体   English

位置:toggleFade()之后的绝对div相对值

[英]making position:absolute div relative after toggleFade()

I've got a project where i've overlaid two imaged and have the top image fade out using a toggleFade function, when the user clicks a toggler (checkbox). 我有一个项目,当用户单击切换器(复选框)时,我已经覆盖了两个图像,并使用toggleFade函数淡出了顶部图像。 it works well, except that to get the images to function correctly the bottom image is set to position:absolute. 它可以正常工作,除了使图像正常运行外,底部图像设置为position:absolute。 Of course, when the toggleFade() happens, the absolute positioning means all the lower divs float up. 当然,当toggleFade()发生时,绝对定位意味着所有较低的div都向上浮动。

    $(document).ready(function(){
    $('.lights').on('click', function (){
        $('.day').fadeToggle(3000);
        setTimeout(function() {$('.night').css('position: absolute');}, 3000);
    });
});

is there any way to prevent this from happening? 有什么办法可以防止这种情况的发生? i've tried setTimeout for the lower div, but that didn't work. 我已经尝试了setTimeout为较低的div,但这没有用。

here's the jsFiddle of my project: https://jsfiddle.net/jsieb81/oue2fnr0/ 这是我的项目的jsFiddle: https ://jsfiddle.net/jsieb81/oue2fnr0/

You can add a class on click event and manage opacity in css with a transition. 您可以在click事件上添加一个类,并通过转换来管理CSS中的不透明度。 Like this : 像这样 :

(You don't need jQuery) (您不需要jQuery)

document.querySelector('.lights').addEventListener('click',function(){
  document.querySelector('.day').classList.add('hide');
});

.hide {
   opacity: 0;
   -webkit-transition: opacity 3s;
   transition: opacity 3s;
  }

see this fiddle https://jsfiddle.net/oue2fnr0/9/ 看到这个小提琴https://jsfiddle.net/oue2fnr0/9/

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

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