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 相关文章推荐
使用IE6看老赵的博客 jQuery初探
Jan 17 Javascript
利用浏览器全屏api实现js全屏
Jan 16 Javascript
JQuery以JSON方式提交数据到服务端示例代码
May 05 Javascript
javascript实现检验的各种规则
Jul 31 Javascript
jQuery实现动态添加和删除一个div
Aug 12 Javascript
Angularjs手动解析表达式($parse)
Oct 12 Javascript
javascript常用的设计模式
Feb 09 Javascript
JS仿淘宝搜索框用户输入事件的实现
Jun 19 Javascript
JsChart组件使用详解
Mar 04 Javascript
实例讲解vue源码架构
Jan 24 Javascript
微信小程序实现原生步骤条
Jul 25 Javascript
ES6基础之 Promise 对象用法实例详解
Aug 22 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
用Socket发送电子邮件(利用需要验证的SMTP服务器)
2006/10/09 PHP
解析PHP强制转换类型及远程管理插件的安全隐患
2014/06/30 PHP
Yii框架中 find findAll 查找出制定的字段的方法对比
2014/09/10 PHP
php实现的简易扫雷游戏实例
2015/07/09 PHP
php生成数字字母的验证码图片
2015/07/14 PHP
php微信开发之上传临时素材
2016/06/24 PHP
php  PATH_SEPARATOR判断当前服务器系统类型实例
2016/10/28 PHP
ThinkPHP类似AOP思想的参数验证的实现方法
2019/12/18 PHP
Laravel5.1 框架路由基础详解
2020/01/04 PHP
JAVASCRIPT代码编写俄罗斯方块网页版
2015/11/26 Javascript
jQuery表格插件datatables用法汇总
2016/03/29 Javascript
JS实现点击网页判断是否安装app并打开否则跳转app store
2016/11/18 Javascript
Vue-resource实现ajax请求和跨域请求示例
2017/02/23 Javascript
js 数字、字符串、布尔值的转换方法(必看)
2017/04/07 Javascript
ES6深入理解之“let”能替代”var“吗?
2017/06/28 Javascript
浅谈箭头函数写法在ReactJs中的使用
2017/08/22 Javascript
vue实现引入本地json的方法分析
2018/07/12 Javascript
小试SVG之新手小白入门教程
2019/01/08 Javascript
js实现GIF动图分解成多帧图片上传
2019/10/24 Javascript
vue+elementUI动态增加表单项并添加验证的代码详解
2020/12/17 Vue.js
vue仿携程轮播图效果(滑动轮播,下方高度自适应)
2021/02/11 Vue.js
python网络编程学习笔记(一)
2014/06/09 Python
Python验证码识别的方法
2015/07/10 Python
python字符串的常用操作方法小结
2016/05/21 Python
python实现飞机大战游戏
2020/10/26 Python
python3 下载网络图片代码实例
2019/08/27 Python
解决pycharm 安装numpy失败的问题
2019/12/05 Python
Python手绘可视化工具cutecharts使用实例
2019/12/05 Python
tensorflow图像裁剪进行数据增强操作
2020/06/30 Python
英国现代市场:ARKET
2019/04/10 全球购物
普通大学毕业生自荐信范文
2014/02/23 职场文书
教师节促销方案
2014/03/22 职场文书
不错的求职信范文
2014/07/20 职场文书
旅游投诉信范文
2015/07/02 职场文书
使用springboot暴露oracle数据接口的问题
2021/05/07 Oracle
Python中zipfile压缩包模块的使用
2021/05/14 Python