I do not want to inherit the child opacity from the parent in CSS

前端 未结 14 900
猫巷女王i
猫巷女王i 2020-11-21 06:31

I do not want to inherit the child opacity from the parent in CSS.

I have one div which is the parent, and I have another div inside the first div which is the child

14条回答
  •  梦如初夏
    2020-11-21 06:58

    My answer is not about static parent-child layout, its about animations.

    I was doing an svg demo today, and i needed svg to be inside div (because svg is created with parent's div width and height, to animate the path around), and this parent div needed to be invisible during svg path animation (and then this div was supposed to animate opacity from 0 to 1, it's the most important part). And because parent div with opacity: 0 was hiding my svg, i came across this hack with visibility option (child with visibility: visible can be seen inside parent with visibility: hidden):

    .main.invisible .test {
      visibility: hidden;
    }
    .main.opacity-zero .test {
      opacity: 0;
      transition: opacity 0s !important;
    }
    .test { // parent div
      transition: opacity 1s;
    }
    .test-svg { // child svg
      visibility: visible;
    }
    

    And then, in js, you removing .invisible class with timeout function, adding .opacity-zero class, trigger layout with something like whatever.style.top; and removing .opacity-zero class.

    var $main = $(".main");
      setTimeout(function() {
        $main.addClass('opacity-zero').removeClass("invisible");
        $(".test-svg").hide();
        $main.css("top");
        $main.removeClass("opacity-zero");
      }, 3000);
    

    Better to check this demo http://codepen.io/suez/pen/54bbb2f09e8d7680da1af2faa29a0aef?editors=011

提交回复
热议问题