jQuery实现公告文字左右滚动的实例代码


Posted in Javascript onOctober 29, 2013
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>jQuery公告文字左右滚动效果-3water.com</title>
<style type="text/css"> 
#scrollText {
    width: 400px;
    margin-right: auto;
    margin-left: auto;
}
</style>
</head>
<script type="text/javascript" src="/source/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
var ScrollTime;
function ScrollAutoPlay(contID,scrolldir,showwidth,textwidth,steper){
    var PosInit,currPos;
    with($('#'+contID)){
        currPos = parseInt(css('margin-left'));
        if(scrolldir=='left'){
            if(currPos<0 && Math.abs(currPos)>textwidth){
                css('margin-left',showwidth);
            }
            else{
                css('margin-left',currPos-steper);
            }
        }
        else{
            if(currPos>showwidth){
                css('margin-left',(0-textwidth));
            }
            else{
                css('margin-left',currPos-steper);
            }
        }
    }
}
//--------------------------------------------左右滚动效果----------------------------------------------
/*
AppendToObj:        显示位置(目标对象)
ShowHeight:        显示高度
ShowWidth:        显示宽度
ShowText:        显示信息
ScrollDirection:    滚动方向(值:left、right)
Steper:        每次移动的间距(单位:px;数值越小,滚动越流畅,建议设置为1px)
Interval:        每次执行运动的时间间隔(单位:毫秒;数值越小,运动越快)
*/
function ScrollText(AppendToObj,ShowHeight,ShowWidth,ShowText,ScrollDirection,Steper,Interval){
    var TextWidth,PosInit,PosSteper;
    with(AppendToObj){
        html('');
        css('overflow','hidden');
        css('height',ShowHeight+'px');
        css('line-height',ShowHeight+'px');
        css('width',ShowWidth);
    }
    if (ScrollDirection=='left'){
        PosInit = ShowWidth;
        PosSteper = Steper;
    }
    else{
        PosSteper = 0 - Steper;
    }
    if(Steper<1 || Steper>ShowWidth){Steper = 1}//每次移动间距超出限制(单位:px)
    if(Interval<1){Interval = 10}//每次移动的时间间隔(单位:毫秒)
    var Container = $('<div></div>');
    var ContainerID = 'ContainerTemp';
    var i = 0;
    while($('#'+ContainerID).length>0){
        ContainerID = ContainerID + '_' + i;
        i++;
    }
    with(Container){
      attr('id',ContainerID);
      css('float','left');
      css('cursor','default');
      appendTo(AppendToObj);
      html(ShowText);
      TextWidth = width();
      if(isNaN(PosInit)){PosInit = 0 - TextWidth;}
      css('margin-left',PosInit);
      mouseover(function(){
          clearInterval(ScrollTime);
      });
      mouseout(function(){
          ScrollTime = setInterval("ScrollAutoPlay('"+ContainerID+"','"+ScrollDirection+"',"+ShowWidth+','+TextWidth+","+PosSteper+")",Interval);
      });
    }
    ScrollTime = setInterval("ScrollAutoPlay('"+ContainerID+"','"+ScrollDirection+"',"+ShowWidth+','+TextWidth+","+PosSteper+")",Interval);
}
</script>
<script type="text/javascript"> 
$(document).ready(function(e) {
    ScrollText($('#scrollText'),23,400,'欢迎光临三水点靠木!','left',1,20);//滚动字幕
});
</script>
<body>
<div id="scrollText"></div>
<script type="text/javascript"> 
if(document.getElementById('GoogleAD')!=null){
    document.getElementById('GoogleAD').innerHTML = '<div class="SearchEngine_AD1">' + document.getElementById('GoogleADCode').innerHTML + '</div>';
}
</script>
</body>
</html>
       
Javascript 相关文章推荐
用jQuery打造TabPanel效果代码
May 22 Javascript
JS写的贪吃蛇游戏(个人练习)
Jul 08 Javascript
常见的javascript跨域通信方法
Dec 31 Javascript
vue router学习之动态路由和嵌套路由详解
Sep 21 Javascript
详解plotly.js 绘图库入门使用教程
Feb 23 Javascript
vue获取元素宽、高、距离左边距离,右,上距离等还有XY坐标轴的方法
Sep 05 Javascript
js中的深浅拷贝问题简析
May 10 Javascript
vue项目中引入Sass实例方法
Aug 27 Javascript
js回溯法计算最佳旅行线路代码实例
Sep 11 Javascript
javascript实现滚轮轮播图片
Dec 13 Javascript
微信小程序组件生命周期的踩坑记录
Mar 03 Javascript
Ajax常用封装库——Axios的使用
May 08 Javascript
javascript查找字符串中出现最多的字符和次数的小例子
Oct 29 #Javascript
JavaScript代码简单实现求杨辉三角给定行的最大值
Oct 29 #Javascript
Js操作Select大全(取值、设置选中等等)
Oct 29 #Javascript
java与javascript之间json格式数据互转介绍
Oct 29 #Javascript
javascript在myeclipse中报错的解决方法
Oct 29 #Javascript
web css实现整站样式互相切换
Oct 29 #Javascript
jquery ajax实现下拉框三级无刷新联动,且保存保持选中值状态
Oct 29 #Javascript
You might like
测试您的 PHP 水平的题目
2007/05/30 PHP
对于Laravel 5.5核心架构的深入理解
2018/02/22 PHP
php实现的支付宝网页支付功能示例【基于TP5框架】
2019/09/16 PHP
PHP实现统计代码行数小工具
2019/09/19 PHP
laravel框架模型中非静态方法也能静态调用的原理分析
2019/11/23 PHP
关于IE7 IE8弹出窗口顶上
2008/12/22 Javascript
5秒后跳转效果(setInterval/SetTimeOut)
2013/05/03 Javascript
js报$ is not a function 的问题的解决方法
2014/01/20 Javascript
jQuery实现单击和鼠标感应事件
2015/02/01 Javascript
javascript实现给定半径求出圆的面积
2015/06/26 Javascript
jqGrid 学习笔记整理——进阶篇(一 )
2016/04/17 Javascript
JavaScript学习小结之被嫌弃的eval函数和with语句实例详解
2016/08/01 Javascript
详解springmvc 接收json对象的两种方式
2016/12/06 Javascript
easyUI combobox实现联动效果
2017/01/17 Javascript
微信小程序 点击控件后选中其它反选实例详解
2017/02/21 Javascript
ionic 3.0+ 项目搭建运行环境的教程
2017/08/09 Javascript
JS实现把一个页面层数据传递到另一个页面的两种方式
2018/08/13 Javascript
JavaScript设计模式之代理模式实例分析
2019/01/16 Javascript
使用vue-router在Vue页面之间传递数据的方法
2019/07/15 Javascript
微信JS-SDK实现微信会员卡功能(给用户微信卡包里发送会员卡)
2019/07/25 Javascript
js的新生代垃圾回收知识点总结
2019/08/22 Javascript
[01:07]2015国际邀请赛 中国区预选赛精彩回顾
2015/06/15 DOTA
Python的Django中将文件上传至七牛云存储的代码分享
2016/06/03 Python
小白入门篇使用Python搭建点击率预估模型
2018/10/12 Python
python中协程实现TCP连接的实例分析
2018/10/14 Python
解决Python print输出不换行没空格的问题
2018/11/14 Python
python3实现字符串操作的实例代码
2019/04/16 Python
python 自动轨迹绘制的实例代码
2019/07/05 Python
Python命名空间namespace及作用域原理解析
2020/06/05 Python
一套PHP的笔试题
2013/05/31 面试题
一位农村小子的自荐信
2014/04/07 职场文书
公司投资建议书
2014/05/16 职场文书
学校食堂标语
2014/10/06 职场文书
银行党员批评与自我批评
2014/10/15 职场文书
护理实习生带教计划
2015/01/16 职场文书
共青团员自我评价
2015/03/10 职场文书