How to fetch the background of DIV on a bottom layer with exact position using jQuery and CSS

前端 未结 7 1565
时光取名叫无心
时光取名叫无心 2021-02-01 23:48

I\'m looking to make a page that has a background gradient that changes color every few seconds and blends between transitions. Now I want to apply this effect on the to the upp

相关标签:
7条回答
  • 2021-02-01 23:59

    You could make the background of the image with the text black, then set the div's background color to rgba(0,0,0,0) making it transparent

    0 讨论(0)
  • 2021-02-02 00:00

    If I was you I'll duplicate the css and jQuery, print it on a div on top of what ever and make the overflow hidden (like masking layers but with z-index).

    0 讨论(0)
  • 2021-02-02 00:04

    This might be helpful for you according to my understanding

    There is inherit to copy a certain value from a parent to its children, but there is no property the other way round (which would involve another selector to decide which style to revert).

    You will have to revert style changes manually:

    div { color: green; }
    
    form div { color: red; }
    
    form div div.content { color: green; }
    

    If you have access to the markup, you can add several classes to style precisely what you need:

    form div.sub { color: red; }
    
    form div div.content { /* remains green */ }
    

    Edit: The CSS Working Group is up to something:

    div.content {
      all: default;
    }
    
    0 讨论(0)
  • 2021-02-02 00:08

    The only way I can see this working is if your black div has no background and is cut into sections that that each have a background. The company name area would need to have the same foreground color as the background for the rest of the div sections. Depending on your layout needs this might be fine.

    For example, you could cut it into three sections and two images: enter image description here

    0 讨论(0)
  • 2021-02-02 00:09

    You can try combinig background-size and background-position with javascript:

    setGradientSizes = function (el) {
    var width = $(document).width() + 'px', height = $(document).height() + 'px';
    $(el || '.gradient:not(body)').each(function () {
        var offset = $(this).offset();
        $(this).css('background-size', width + ' ' + height);
        $(this).css('background-position', (offset.left * -1) + 'px ' + (offset.top * -1) + 'px');
    })};
    

    Working example here -> jsbin

    NOTES:

    • this is not 100% cross browser - background-size is supported in FF4.0+, IE9.0+, Opera 10.0+, Chrome 1.0+, Safari 3+.
    • For some older browsers you can try browser specific prefixes (like -moz-background-size) - my example does not cover that.
    • To reduce load flickering you can apply calculations at first and then add background gradient
    0 讨论(0)
  • 2021-02-02 00:10

    Since you ask for alternatives to jQuery solutions

    You could play a little with margins and box-shadow and keyframe animations.

    Something in this direction for the shape (depends on what you want to do with which part - add content ... and in what way you want it to be responsive):

    html:

    <div class="wrapper">
        <div class="header"><img src="http://i.imgur.com/CUbOIxr.png" alt="Company name" /></div>
        <div class="content"></div>
    </div>
    

    CSS:

    body {
        background:orange;
        width:100%;
        height:100%;
    }
    .wrapper {
        width:40%;
        height:90%;
        border:30px solid #000;
        border-right-width:100px;
        border-bottom-width:100px;
    }
    .header {
        width:100%;
        border-bottom:10px solid transparent;
        -webkit-box-shadow: 0 30px 0 #000;
        -moz-box-shadow: 0 30px 0 #000;
        box-shadow: 0 30px 0 #000;
    }
    .header img {
        width:100%;
    }
    .content {
        width:95%;
        height:400px;
        background-color:#000;
        margin-top:30px;
    }
    

    DEMO

    This way no javascript is needed. And for the background you can use a linear gradient and do all animations with css transitions or keyframe animations. You also need to play with the lengths and adjust the borders and box-shadows to your needs, maybe add some @media queries for the responsiveness.

    Hope this helps you a little in the right direction =)


    Update:

    I hoped the gradients changing was the smaller problem ;-) Silly me, sorry.

    I will elaborate my CSS-only suggestion for the animation, but you can choose a javascript slider for the background animation, if you don't like CSS3 solutions - although this is the hot stuff now ;-)

    Ok. So, I would add some more fixed positioned elements with gradient backgrounds (layer1 and layer2).

    To have something in this direction in the html now:

    <div class="layer layer1"></div>
    <div class="layer layer2"></div>
    <div class="wrapper">
        <div class="header">
            <img src="http://newtpond.com/test/company-name.png" alt="Company name" />
        </div>
        <div class="content"></div>
    </div>
    

    and add a keyframe animation on them in CSS (here it is just with the -webkit vendor prefix [probably cause I am a lazy bum], but I hope you can get the idea, and could add the others):

    body {
        width:100%;
        height:100%;
        margin:0;
        padding:0;
    }
    /* for the animation */
     .layer {
        position:fixed;
        width:100%;
        height:100%;
    }
    @-webkit-keyframes GoLayer1 {
        0% {
            opacity:1;
        }
        50% {
            opacity:0;
        }
        100% {
            opacity:1;
        }
    }
    @-webkit-keyframes GoLayer2 {
        0% {
            opacity:0;
        }
        50% {
            opacity:1;
        }
        100% {
            opacity:0;
        }
    }
    .layer1 {
        background: -webkit-linear-gradient(bottom, rgb(43, 70, 94) 29%, rgb(194, 41, 41) 65%, rgb(155, 171, 38) 83%);
        -webkit-animation: GoLayer1 5s infinite;
    }
    .layer2 {
        background: -webkit-linear-gradient(bottom, rgb(225, 202, 230) 29%, rgb(39, 163, 194) 65%, rgb(36, 124, 171) 83%);
        -webkit-animation: GoLayer2 5s infinite;
    }
    /* the wrapper shape */
     .wrapper {
        z-index:999;
        opacity:1;
        position:relative;
        width:40%;
        height:90%;
        border:30px solid #000;
        border-right-width:100px;
        border-bottom-width:100px;
    }
    .header {
        width:100%;
        border-bottom:10px solid transparent;
        -webkit-box-shadow: 0 30px 0 #000;
        -moz-box-shadow: 0 30px 0 #000;
        box-shadow: 0 30px 0 #000;
    }
    .header img {
        width:100%;
    }
    .content {
        width:95%;
        height:400px;
        background-color:#000;
        margin-top:28px;
    }
    

    DEMO (tested in Chrome 26 - looked cool =)

    This is now where I can point you according this CSS-only approach. There is still stuff to modify and consider browser compatibility. But it is certainly an alternative ... and a step in the direction where html5 and css3 is going (if you want to be hot and cool ;-), hehe, sorry, too much silliness.

    Good luck!


    Update 2:

    So, I overcame my laziness a tiny bit and added some more vendor prefixes to the top example (and of course you can use any image as background):

    DEMO

    And here I add another example, that is using a png image for the gradient, and is sliding up and down in the background (as another alternative):

    DEMO

    0 讨论(0)
提交回复
热议问题