CSS-moving text from left to right

前端 未结 6 581
自闭症患者
自闭症患者 2020-12-19 05:52

I want to create an animated HTML \"marquee\" that scrolls back and forth on a website:

This is a marquee!
相关标签:
6条回答
  • 2020-12-19 06:29

    If I understand you question correctly, you could create a wrapper around your marquee and then assign a width (or max-width) to the wrapping element. For example:

    <div id="marquee-wrapper">
        <div class="marquee">This is a marquee!</div>   
    </div>
    

    And then #marquee-wrapper { width: x }.

    0 讨论(0)
  • 2020-12-19 06:30

    I like using the following to prevent things being outside my div elements. It helps with CSS rollovers too.

    .marquee{
        overflow:hidden;
    }
    

    this will hide anything that moves/is outside of the div which will prevent the browser expanding and causing a scroll bar to appear.

    0 讨论(0)
  • 2020-12-19 06:32

    Somehow I got it to work by using margin-right, and setting it to move from right to left. http://jsfiddle.net/gXdMc/

    Don't know why for this case, margin-right 100% doesn't go off the screen. :D (tested on chrome 18)

    EDIT: now left to right works too http://jsfiddle.net/6LhvL/

    0 讨论(0)
  • 2020-12-19 06:33

    I am not sure if this is the correct solution but I have achieved this by redefining .marquee class just after animation CSS.

    Check below:

    <style>
    #marquee-wrapper{
        width:700px;
        display:block;
        border:1px solid red;
    }
    
    div.marquee{
    width:100px;
    height:100px;
    background:red;
    position:relative;
    animation:myfirst 5s;
    -moz-animation:myfirst 5s; /* Firefox */
    }
    
    @-moz-keyframes myfirst /* Firefox */{
    0%   {background:red; left:0px; top:0px;}
    100% {background:red; left:100%; top:0px}
    }
    div.marquee{ 
    left:700px; top:0px
    }
    </style>
    
    <!-- HTMl COde -->
    
    <p><b>Note:</b> This example does not work in Internet Explorer and Opera.</p>
    <div id="marquee-wrapper">
    <div class="marquee"></div>
    
    0 讨论(0)
  • 2020-12-19 06:38

    Hi you can achieve your result with use of <marquee behavior="alternate"></marquee>

    HTML

    <div class="wrapper">
    <marquee behavior="alternate"><span class="marquee">This is a marquee!</span></marquee>
    </div>
    

    CSS

    .wrapper{
        max-width: 400px;
        background: green;
        height: 40px;
        text-align: right;
    }
    
    .marquee {
        background: red;
        white-space: nowrap;
        -webkit-animation: rightThenLeft 4s linear;
    }
    

    see the demo:- http://jsfiddle.net/gXdMc/6/

    0 讨论(0)
  • 2020-12-19 06:51

    You could simply use CSS animated text generator. There are pre-created templates already

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