不用Margin使用jQuery实现左右滚动效果
来源:互联网 发布:月薪一万 知乎 编辑:程序博客网 时间:2024/06/10 19:02
相信不少网友们从前也使用过HTML中的Marquee来实现左右滚动的效果,但后来随着WEB标准的规范法,逐步用JS来替代这个传统的做法了。最近工作的需要,在网上参考了一下各路高手的做法,其中用得最多的应该是利用CSS中的position:absolute方法来实现。而本人就在前人的基础上改进优化了一下,使用jQuery+CSS中的margin-left方法,并将定义成果封装好直接调用即可。
最后把核心源码写出来给大家参考参考——
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);
}
打开演示地址
最后把核心源码写出来给大家参考参考——
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);
}
- 不用Margin使用jQuery实现左右滚动效果
- jquery实现图片左右滚动效果
- JavaScript中用Jquery实现左右点击滚动效果
- jQuery左右滚动效果(带停顿)
- jquery使用div实现滚动条效果
- jQuery实现滚动效果
- Jquery 滚动效果实现
- Android的实现左右滚动效果
- 基于jQuery的控制左右滚动及自动滚动效果
- 基于jQuery的控制左右滚动及自动滚动效果
- 基于jQuery的控制左右滚动效果_自动滚动
- 基于jquery的循环左右滚动和上下滚动效果
- Flex中不用ScrollBar实现滚动效果
- jquery插件实现图片的左右滚动
- jQuery实现一个图片左右滚动
- jquery实现图片的左右滚动
- Jquery实现图片左右滚动(自动)
- jquery实现单行滚动效果
- 汉字转拼音。简繁体互转。ChnCharInfo.dll,ChineseConverter.dll
- String资源文件的format方法
- C/C++基础知识点全面汇总(计算机2级考试,程序员考试 基础考点)
- jQuery密码强度插件passwordStrength的实例演示
- 屏幕蒙层效果、图层对象绝对居中、拖拽图层对象三个功能实例演示剖析
- 不用Margin使用jQuery实现左右滚动效果
- Jquery实现无限级树状结构并动态添加增删改等编辑功能
- 别样的面试
- 怎么学算法
- 刘道成 mysql 学习笔记2
- 看 血战长空,心里难受的很
- 程序员笑话
- 不用alert弹出窗口的方法来显示验证信息
- 汉诺塔 简化版 c++