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 html()等方法介绍
Nov 18 Javascript
jquery实现邮箱自动补全功能示例分享
Feb 17 Javascript
javascript页面上使用动态时间具体实现
Mar 18 Javascript
JavaScript中exec函数用法实例分析
Jun 08 Javascript
JSON遍历方式实例总结
Dec 07 Javascript
JavaScript提高网站性能优化的建议(二)
Jul 24 Javascript
setTimeout学习小结
Feb 08 Javascript
Vue2.0组件间数据传递示例
Mar 07 Javascript
详解react-router如何实现按需加载
Jun 15 Javascript
vue cli升级webapck4总结
Apr 04 Javascript
vue.js 中使用(...)运算符报错的解决方法
Aug 09 Javascript
js隐式转换的知识实例讲解
Sep 28 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
在smarty模板中使用PHP函数的方法
2011/04/23 PHP
ThinkPHP中html:list标签用法分析
2016/01/09 PHP
PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)
2016/09/11 PHP
php中final关键字用法分析
2016/12/07 PHP
Laravel 使用查询构造器配合原生sql语句查询的例子
2019/10/12 PHP
jquery的extend和fn.extend的使用说明
2011/01/09 Javascript
面向对象的Javascript之一(初识Javascript)
2012/01/20 Javascript
JS图片根据鼠标滚动延时加载的实例代码
2013/07/13 Javascript
jQuery学习笔记之总体架构
2014/06/03 Javascript
jquery数组过滤筛选方法grep()简介
2014/06/06 Javascript
函数window.open实现关闭所有的子窗口
2015/08/03 Javascript
js电话号码验证方法
2015/09/28 Javascript
js实现创建删除html元素小结
2015/09/30 Javascript
prototype框架中美元符号$用法分析
2016/01/22 Javascript
JS实现把鼠标放到链接上出现滚动文字的方法
2016/04/06 Javascript
jQuery基于BootStrap样式实现无限极地区联动
2016/08/26 Javascript
详解nodejs异步I/O和事件循环
2017/06/07 NodeJs
shiro授权的实现原理
2017/09/21 Javascript
详解Eslint 配置及规则说明
2018/09/10 Javascript
vue中接口域名配置为全局变量的实现方法
2018/09/20 Javascript
vue通过数据过滤实现表格合并
2020/11/30 Javascript
layui框架与SSM前后台交互的方法
2019/09/12 Javascript
在Vue中使用CSS3实现内容无缝滚动的示例代码
2020/11/27 Vue.js
[01:09:19]DOTA2-DPC中国联赛 正赛 VG vs Aster BO3 第二场 2月28日
2021/03/11 DOTA
Python程序设计入门(1)基本语法简介
2014/06/13 Python
Python入门篇之编程习惯与特点
2014/10/17 Python
python转换字符串为摩尔斯电码的方法
2015/07/06 Python
[原创]教女朋友学Python(一)运行环境搭建
2017/11/29 Python
使用python中的in ,not in来检查元素是不是在列表中的方法
2018/07/06 Python
Python根据当前日期取去年同星期日期
2019/04/14 Python
Python re 模块findall() 函数返回值展现方式解析
2019/08/09 Python
python集合常见运算案例解析
2019/10/17 Python
pandas中ix的使用详细讲解
2020/03/09 Python
澳大利亚家庭花园和DIY工具网店:VidaXL
2019/05/03 全球购物
教师队伍管理制度
2014/01/14 职场文书
小学作文之描写天气
2019/08/15 职场文书