jquery / css: make text inside div scroll horizontally like a news ticker no plugin

后端 未结 4 1939
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 06:26

anyone have some tips on how to make text inside a div scroll horizontally from right to left in a \"news ticker\" fashion without having to use a plugin. Here is an example of

相关标签:
4条回答
  • 2021-02-09 06:54

    you could do something like this

    <div id="scrollcontainer" style="postion:relative; overflow:hidden; width:100%;">
      <span id="scrolltext" style="position:absolute; white-space:nowrap">this is the scrolling text</span>
    </div>
    

    and a js function

    function scroll() {
      $('#scrolltext').css('left', $('#scrollcontainer').width());
      $('#scrolltext').animate({
        left: '-='+($('#scrollcontainer').width()+$('#scrolltext').width())
      }, 2000, function() {
        scroll();
      });
    }
    
    scroll();
    

    tested. can be found here: http://jsfiddle.net/zrW5q/85/

    0 讨论(0)
  • 2021-02-09 06:57

    To make it scrolling from bottom to top, replace the left attribute with top. That's worked for me

    0 讨论(0)
  • 2021-02-09 07:03

    Here's a quick solution to this: http://jsfiddle.net/4mTMw/8/

    var marquee = $('div.marquee');
    marquee.each(function() {
        var mar = $(this),indent = mar.width();
        mar.marquee = function() {
            indent--;
            mar.css('text-indent',indent);
            if (indent < -1 * mar.children('div.marquee-text').width()) {
                indent = mar.width();
            }
        };
        mar.data('interval',setInterval(mar.marquee,1000/60));
    });​
    

    Using the text-indent css property, you can do this rather simply.

    0 讨论(0)
  • 2021-02-09 07:06

    I recently solved this with CSS keyframe animations.

    Essentially your ticker needs a wrapper div with overflow hidden on it. The tickers contained items should display inline-block so they are in a line:

    <div class="ticker-wrap">
        <div class="ticker">
            <div class="ticker__item">Letterpress chambray brunch.</div>
            <div class="ticker__item">Vice mlkshk crucifix beard chillwave meditation hoodie asymmetrical Helvetica.</div>
            <div class="ticker__item">Ugh PBR&B kale chips Echo Park.</div>
        </div>
    </div>
    

    Example CSS:

    .ticker-wrap {
        position: fixed;
        bottom: 0;
        width: 100%;
        overflow: hidden;
        height: 4rem;
        background-color: rgba(51, 51, 51, 0.5);
        padding-left: 100%; // offsets items to begin
    }
    
    .ticker {
        display: inline-block;
        height: 4rem;
        line-height: 4rem;
        white-space: nowrap;
        padding-right: 100%; // taken from container as display inline-block
    }
    
    .ticker__item {
        display: inline-block;
        padding: 0 2rem;
        font-size: 2rem;
        color: white;
    }
    

    I have a demo that uses the css keyframe animation to then transform the content all the way from one side to the other infinitely. n.b. Vendor prefixed versions not shown.

    @keyframes ticker {
        0% {
            -webkit-transform: translate3d(0, 0, 0);
                    transform: translate3d(0, 0, 0);
    
        }
        100% {
            -webkit-transform: translate3d(-100%, 0, 0);
                    transform: translate3d(-100%, 0, 0);
        }
    }
    

    The only tweak you need is to set the animation duration and apply it to .ticker.

    .ticker {
        animation-name: ticker;
        animation-iteration-count: infinite;
        animation-timing-function: linear;
        animation-duration: 25s; // tweak based on number of items/desired speed
    }
    

    You can see the full demo

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