Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

javascript - How to display only part of a background image: linear gradient?

I'm curious nobody asked this question yet:

I'm creating a progress bar as a react component. I want to fill this progress bar with a linear gradient. But I also want to display only the completed percentage of that background-color.

I didn't find how to resolve that. Here is my CSS code:

.ProgressBarContainer{
    height: 24px;
    width: 250px;
    background-color: lightgray;
}

.ProgressBarGradient{
    height: 24px;
    width: 75%;
    background-image: linear-gradient(to right, #a83e4c , #489668);
}

.ProgressBarFiller{
    height: 24px;
    width: 75%;
}

And here is the JS:

const ProgressBar = (props)=>{
    return(
        <div className={classes.ProgressBarContainer}>
            <div className={classes.ProgressBarGradient}>
                <div className={classes.ProgressBarFiller}></div>
            </div>
        </div>
    )
}

Here is what is displayed at the moment

I want, in fact, to display only red as long as I'm under 20% width, And to display the green only if I approach the 100% ..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to the comment, the solution is to use the 3rd div to hide part of the 2nd div.

.GradientHider{
    height: 24px;
    margin-left:50%;
    background-color: lightgray;
}

And I realize that the first div was useless ..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...