Front-end daily combat: # 21 video slide shows how the effects of UI interface using pure CSS text creation

image description

Results preview

Press the right "click the Preview" button to preview the current page, click on the link to full-screen preview.

https://codepen.io/comehope/pen/QrxxaW

Interactive video tutorials

This video is interactive, you can pause the code video, editing video.

Please use chrome, safari, edge open view.

https://scrimba.com/p/pEgDAM/cgRgQHr

Source code download

Please download from github.

https://github.com/comehope/front-end-daily-challenges/tree/master/021-a-text-sliding-effect-ui

Code Reading

Defined dom, the container comprising a question and answer:

<div>
    <p>
        <span class="question">Who gives you milk?</span>
        <span class="answer">cow</span>
    </p>
</div>

Centered:

html, body {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: darkcyan;
}

Setting text style:

p {
    width: 400px;
    height: 2.5em;
    color: gold;
    font-size: 24px;
    border: 2px solid gold;
    line-height: 2.5em;
    text-align: center;
    border-radius: 10px;
    font-family: sans-serif;
    letter-spacing: 2px;
    word-spacing: 2px;
    box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.2);
}

Set answer style:

p {
    position: relative;
}

p span {
    position: absolute;
    width: 100%;
    top: 0;
    left: 0;
}

p .answer {
    color: whitesmoke;
    font-size: 1.1em;
    text-transform: uppercase;
    background: rgba(0, 0, 0, 0.1);
}

Increase in questions and answers sliding switch animation:

p {
    overflow: hidden;
}

p span {
    transition: 0.5s ease-out;
}

p .question {
    left: 0;
}

p:hover .question {
    left: 100%;
}

p .answer {
    left: -100%;
}

p:hover .answer {
    left: 0;
}

dom in to add two questions:

<div>
    <p>
        <span class="question">Who gives you milk?</span>
        <span class="answer">cow</span>
    </p>
    <p>
        <span class="question">Who likes to eat flies?</span>
        <span class="answer">frog</span>
    </p>
    <p>
        <span class="question">Who have large claws?</span>
        <span class="answer">crab</span>
    </p>
</div>

dom added the title:

<div>
    <h1>Who Am I</h1>
    <p>
        <span class="question">Who gives you milk?</span>
        <span class="answer">cow</span>
    </p>
    <p>
        <span class="question">Who likes to eat flies?</span>
        <span class="answer">frog</span>
    </p>
    <p>
        <span class="question">Who have large claws?</span>
        <span class="answer">crab</span>
    </p>
</div>

Set heading styles:

h1 {
    font-family: sans-serif;
    color: gold;
    text-align: center;
}

Finally, the big question mark as a pseudo-elements increase decorative background:

h1 {
    position: relative;
}

h1::after {
    content: '?';
    position: absolute;
    top: -0.35em;
    left: 0;
    font-size: 25em;
    transform: rotate(15deg);
    color: cyan;
    filter: opacity(0.3);
    text-shadow: 10px 10px 10px rgba(0, 0, 0, 0.2);
}

Knowledge Point

Guess you like

Origin www.cnblogs.com/jlfw/p/11921828.html