您好,欢迎访问一九零五行业门户网

css文字从右边到左的滚动效果怎么实现?(代码示例)

本篇文章主要给大家介绍网页html文字滚动代码效果如何写?当我们在浏览新闻播放的页面时,总会看到底部有一段实时新闻不停的滚动出现,这样的效果通常可以由js或者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() 定义动画
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滚动效果 文字横向滚动和js文字滚动的方法介绍,希望对有需要的朋友有所帮助。
【相关文章推荐】
html中制作滚动文字的实例代码
10行js实现文字无缝滚动简单方法
html怎样借助marquee实现文字左右滚动
js实现文字间歇循环滚动
以上就是css文字从右边到左的滚动效果怎么实现?(代码示例)的详细内容。
其它类似信息

推荐信息