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 相关文章推荐
jBox 2.3基于jquery的最新多功能对话框插件 常见使用问题解答
Nov 10 Javascript
js实现右下角可关闭最小化div(可用于展示推荐内容)
Jun 24 Javascript
Jquery Post处理后不进入回调的原因及解决方法
Jul 15 Javascript
js实现用户离开页面前提示是否离开此页面的方法(包括浏览器按钮事件)
Jul 18 Javascript
基于javascript实现简单的抽奖系统
Apr 15 Javascript
手机Web APP如何实现分享多平台功能
Aug 19 Javascript
微信小程序 获取设备信息 API实例详解
Oct 02 Javascript
Vue.js实现表格动态增加删除的方法(附源码下载)
Jan 20 Javascript
你可能不知道的前端算法之文字避让(inMap)
Jan 12 Javascript
解决vue 更改计算属性后select选中值不更改的问题
Mar 02 Javascript
详解vue-cli脚手架中webpack配置方法
Aug 22 Javascript
package.json中homepage属性的作用详解
Mar 11 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
摩卡咖啡
2021/03/03 咖啡文化
php+oracle 分页类
2006/10/09 PHP
php购物车实现代码
2011/10/10 PHP
php提示Failed to write session data错误的解决方法
2014/12/17 PHP
thinkPHP框架实现生成条形码的方法示例
2018/06/06 PHP
php实现生成PDF文件的方法示例【基于FPDF类库】
2018/07/21 PHP
js 处理URL实用技巧
2010/11/23 Javascript
jQuery判断元素是否是隐藏的代码
2011/04/24 Javascript
浅析return false的正确使用
2013/11/04 Javascript
jQuery实现ichat在线客服插件
2014/12/29 Javascript
zepto中使用swipe.js制作轮播图附swipeUp,swipeDown不起效果问题
2015/08/27 Javascript
jQuery插件之Tocify动态节点目录菜单生成器附源码下载
2016/01/08 Javascript
AngularJS用户选择器指令实例分析
2016/11/04 Javascript
JavaScript 过滤关键字
2017/03/20 Javascript
手机注册发送验证码倒计时的简单实例
2017/11/15 Javascript
深入理解Vue父子组件生命周期执行顺序及钩子函数
2018/08/12 Javascript
使用vue-cli webpack 快速搭建项目的代码
2018/11/21 Javascript
原生JavaScript之es6中Class的用法分析
2020/02/23 Javascript
微信小程序实现带放大效果的轮播图
2020/05/26 Javascript
vue实现图片上传到后台
2020/06/29 Javascript
Vue中正确使用Element-UI组件的方法实例
2020/10/13 Javascript
Python Web服务器Tornado使用小结
2014/05/06 Python
python访问mysql数据库的实现方法(2则示例)
2016/01/06 Python
python基于phantomjs实现导入图片
2016/05/13 Python
Django重装mysql后启动报错:No module named ‘MySQLdb’的解决方法
2018/04/22 Python
python中的decorator的作用详解
2018/07/26 Python
利用setuptools打包python程序的方法步骤
2020/01/18 Python
基于python计算滚动方差(标准差)talib和pd.rolling函数差异详解
2020/06/08 Python
python subprocess pipe 实时输出日志的操作
2020/12/05 Python
韩国流行时尚女装网站:Dintchina(中文)
2018/07/19 全球购物
我想声明一个指针并为它分配一些空间, 但却不行。这些代码有什么 问题?char *p; *p = malloc(10);
2016/10/06 面试题
数控技术应用个人求职信范文
2014/02/03 职场文书
鼓舞士气的口号
2014/06/16 职场文书
如何用JavaScript实现一个数组惰性求值库
2021/05/05 Javascript
浅谈Vue的computed计算属性
2022/03/21 Vue.js
集英社今正式宣布 成立游戏公司“集英社Games”
2022/03/31 其他游戏