如何实现css文字从右边到左的滚动效果

发布时间:2020-09-23 15:02:38 作者:小新
来源:亿速云 阅读:318

这篇文章主要介绍了如何实现css文字从右边到左的滚动效果,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

一、js文字滚动代码具体示例:

HTML代码 :

<!DOCTYPE HTML>
<html lang="en">
<head>
    <title></title>
    <meta charset="UTF-8">
    <style type="text/css">
    </style>
</head>
<body>
<div class="container">
    <p class="text">文字从右到左滚动 css文字从右到左滚动 css文字从右到左滚动 css文字从右到左滚动 css文字从右到左滚动 css</p>
</div>
</body>
</html>
<script>
  var $container = $('.container'),
    $text = $('.text');  var direction = 1, 
    speed = 3000; 
  var textMove = function (obj, container, direction, speed) {
    var initMarginLeft = '-' + obj.width() + 'px';
    obj.css({"margin-left": initMarginLeft});  
    var move = function () {
      var allDistance = obj.width() + container.width(),
        remainedDistance = container.width() - parseInt(obj.css('margin-left')),
        currentSpeed = (speed * remainedDistance ) / allDistance;      // 移动效果
      obj.animate({"margin-left": container.width() + 'px'}, currentSpeed, 'linear', function () {
        obj.stop(true, true);
        obj.css({"margin-left": initMarginLeft});
        move();
      });
    };
    move();   
    container.on("mouseenter", function () {obj.stop(true)})
      .on('mouseleave', function () {move()})
  };
  textMove($text, $container, direction, speed);</script>

以上文字滚动js代码中相关知识点注释:

var direction中表示 1为从左进入,2为从右进入;

speed 表示数值越小速度越快

var textMove,定义文字初始位置

obj.css()  定义动画

如何实现css文字从右边到左的滚动效果animate() 方法执行 CSS 属性集的自定义动画。

该方法通过CSS样式将元素从一个状态改变为另一个状态。CSS属性值是逐渐改变的,这样就可以创建动画效果。只有数字值可创建动画(比如 "margin:30px")。字符串值无法创建动画(比如 "background-color:red")。

二、css文字在div里滚动代码示例:

  <style type="text/css" rel="stylesheet">
    * {  margin: 0;  padding: 0;}
    .container { margin: 200px auto;  width: 500px;   height: 50px; line-height: 50px;border: 1px solid red;  overflow: hidden;    }
    .text {  position: relative;      display: inline-block;      white-space: nowrap;      /*animation:scroll 5s 0s linear infinite;*/
      animation-name: scroll;      animation-duration: 5s;      animation-delay: 0ms;      animation-timing-function: linear;      animation-iteration-count: infinite;      -webkit-animation-name: scroll;      -webkit-animation-delay: 0ms;      -webkit-animation-duration: 5s;      -webkit-animation-timing-function: linear;      -webkit-animation-iteration-count: infinite;      -moz-animation-name: scroll;      -moz-animation-delay: 0ms;      -moz-animation-duration: 5s;      -moz-animation-timing-function: linear;      -moz-animation-iteration-count: infinite;    }
    @-webkit-keyframes scroll {
      100% {  margin-left: 100%; }
    }    @-moz-keyframes scroll {
      100%  {  margin-left: 100%;}
    }    @-ms-keyframes scroll {
      100%  { margin-left: 100%;  }
    }    .text:hover { -webkit-animation-play-state: paused;  }
  </style>

相关知识点注释:

通过 @keyframes 规则,您能够创建动画。原理是,将一套 CSS 样式逐渐变化为另一套样式。在动画过程中,您能够多次改变这套 CSS 样式。以百分比来规定改变发生的时间,或者通过关键词 "from" 和 "to",等价于 0% 和 100%。0% 是动画的开始时间,100% 动画的结束时间。为了获得最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。

animationname    必需。定义动画的名称。

keyframes-selector    必需。动画时长的百分比。

合法的值:0-100% from(与 0% 相同)to(与 100% 相同)

css-styles    必需。一个或多个合法的 CSS 样式属性。

感谢你能够认真阅读完这篇文章,希望小编分享如何实现css文字从右边到左的滚动效果内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

推荐阅读:
  1. HTML实现文字滚动效果的方法
  2. 左浮动如何用css实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

html 如何实现

上一篇:C++  数据结构链表的实现代码

下一篇:如何解决angularjs内存溢出

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》