Liu Zichen (Talk | contribs) (Created page with "var dir = 1; //每步移动像素,大=快 var speed = 10; //循环周期(毫秒)大=慢 var MyMar = null; function Marquee() { //正常移动 var demo = docu...") |
Liu Zichen (Talk | contribs) |
||
Line 1: | Line 1: | ||
− | var dir = 1; | + | var dir = 1; |
− | var speed = 10; | + | var speed = 10; |
var MyMar = null; | var MyMar = null; | ||
− | function Marquee() { | + | function Marquee() { |
var demo = document.getElementById("demo"); | var demo = document.getElementById("demo"); | ||
var demo2 = document.getElementById("demo2"); | var demo2 = document.getElementById("demo2"); | ||
Line 15: | Line 15: | ||
function onmouseoverMy() { | function onmouseoverMy() { | ||
window.clearInterval(MyMar); | window.clearInterval(MyMar); | ||
− | } | + | } |
function onmouseoutMy() { | function onmouseoutMy() { | ||
MyMar = setInterval(Marquee, speed); | MyMar = setInterval(Marquee, speed); | ||
− | } | + | } |
function r_left() { | function r_left() { | ||
if (dir == -1) dir = 1; | if (dir == -1) dir = 1; | ||
− | } | + | } |
function r_right() { | function r_right() { | ||
if (dir == 1) dir = -1; | if (dir == 1) dir = -1; | ||
− | } | + | } |
function IsIE() { | function IsIE() { | ||
var browser = navigator.appName | var browser = navigator.appName | ||
Line 47: | Line 47: | ||
_MousePX = _IsIE ? (document.body.scrollLeft + event.clientX) : e.pageX; | _MousePX = _IsIE ? (document.body.scrollLeft + event.clientX) : e.pageX; | ||
_MousePY = _IsIE ? (document.body.scrollTop + event.clientY) : e.pageY; | _MousePY = _IsIE ? (document.body.scrollTop + event.clientY) : e.pageY; | ||
− | + | ||
var obj1 = null; | var obj1 = null; | ||
if (obj.getBoundingClientRect) { | if (obj.getBoundingClientRect) { | ||
− | + | ||
obj1 = document.getElementById("demo").getBoundingClientRect(); | obj1 = document.getElementById("demo").getBoundingClientRect(); | ||
_DivLeft = obj1.left; | _DivLeft = obj1.left; | ||
Line 56: | Line 56: | ||
_AllDivWidth = _DivRight - _DivLeft; | _AllDivWidth = _DivRight - _DivLeft; | ||
} else if (document.getBoxObjectFor) { | } else if (document.getBoxObjectFor) { | ||
− | + | ||
obj1 = document.getBoxObjectFor(obj); | obj1 = document.getBoxObjectFor(obj); | ||
var borderwidth = (obj.style.borderLeftWidth != null && obj.style.borderLeftWidth != "") ? parseInt(obj.style.borderLeftWidth) : 0; | var borderwidth = (obj.style.borderLeftWidth != null && obj.style.borderLeftWidth != "") ? parseInt(obj.style.borderLeftWidth) : 0; | ||
Line 63: | Line 63: | ||
_DivRight = _DivLeft + _AllDivWidth; | _DivRight = _DivLeft + _AllDivWidth; | ||
} else { | } else { | ||
− | + | ||
_DivLeft = obj.offsetLeft; | _DivLeft = obj.offsetLeft; | ||
_AllDivWidth = Cut_Px(obj.style.width); | _AllDivWidth = Cut_Px(obj.style.width); | ||
Line 81: | Line 81: | ||
if (_MousePX > _DivLeft && _MousePX < _DivRight) { | if (_MousePX > _DivLeft && _MousePX < _DivRight) { | ||
− | if (_MousePX > _DivLeft && _MousePX < pos1) | + | if (_MousePX > _DivLeft && _MousePX < pos1) |
{ | { | ||
r_left(); | r_left(); | ||
− | } else if (_MousePX < _DivRight && _MousePX > pos2) | + | } else if (_MousePX < _DivRight && _MousePX > pos2) |
{ | { | ||
r_right(); | r_right(); | ||
} | } | ||
− | if (_MousePX > pos1 && _MousePX < pos2) | + | if (_MousePX > pos1 && _MousePX < pos2) |
{ | { | ||
onmouseoverMy(); | onmouseoverMy(); |
Latest revision as of 08:44, 16 August 2017
var dir = 1; var speed = 10; var MyMar = null; function Marquee() {
var demo = document.getElementById("demo"); var demo2 = document.getElementById("demo2"); if (dir > 0 && (demo2.offsetWidth - demo.scrollLeft) <= 0) { demo.scrollLeft = 0; } if (dir < 0 && (demo.scrollLeft <= 0)) { demo.scrollLeft = demo2.offsetWidth; } demo.scrollLeft += dir;
} function onmouseoverMy() {
window.clearInterval(MyMar);
} function onmouseoutMy() {
MyMar = setInterval(Marquee, speed);
} function r_left() {
if (dir == -1) dir = 1;
} function r_right() {
if (dir == 1) dir = -1;
} function IsIE() {
var browser = navigator.appName if ((browser == "Netscape")) { return false; } else if (browser == "Microsoft Internet Explorer") { return true; } else { return null; }
}
var _IsIE = IsIE(); var _MousePX = 0; var _MousePY = 0; var _DivLeft = 0; var _DivRight = 0; var _AllDivWidth = 0; var _AllDivHeight = 0; function MoveDiv(e) {
var obj = document.getElementById("demo"); _MousePX = _IsIE ? (document.body.scrollLeft + event.clientX) : e.pageX; _MousePY = _IsIE ? (document.body.scrollTop + event.clientY) : e.pageY; var obj1 = null; if (obj.getBoundingClientRect) { obj1 = document.getElementById("demo").getBoundingClientRect(); _DivLeft = obj1.left; _DivRight = obj1.right; _AllDivWidth = _DivRight - _DivLeft; } else if (document.getBoxObjectFor) { obj1 = document.getBoxObjectFor(obj); var borderwidth = (obj.style.borderLeftWidth != null && obj.style.borderLeftWidth != "") ? parseInt(obj.style.borderLeftWidth) : 0; _DivLeft = parseInt(obj1.x) - parseInt(borderwidth); _AllDivWidth = Cut_Px(obj.style.width); _DivRight = _DivLeft + _AllDivWidth; } else { _DivLeft = obj.offsetLeft; _AllDivWidth = Cut_Px(obj.style.width); var parent = obj.offsetParent; if (parent != obj) { while (parent) { _DivLeft += parent.offsetLeft; parent = parent.offsetParent; } } _DivRight = _DivLeft + _AllDivWidth; }
var pos1, pos2; pos1 = parseInt(_AllDivWidth * 0.4) + _DivLeft; pos2 = parseInt(_AllDivWidth * 0.6) + _DivLeft;
if (_MousePX > _DivLeft && _MousePX < _DivRight) { if (_MousePX > _DivLeft && _MousePX < pos1) { r_left(); } else if (_MousePX < _DivRight && _MousePX > pos2) { r_right(); }
if (_MousePX > pos1 && _MousePX < pos2) { onmouseoverMy(); MyMar = null; } else if (_MousePX < pos1 || _MousePX > pos2) { if (MyMar == null) { MyMar = setInterval(Marquee, speed); } } }
}
function Cut_Px(cswidth) {
cswidth = cswidth.toLowerCase(); if (cswidth.indexOf("px") != -1) { cswidth.replace("px", ""); cswidth = parseInt(cswidth); } return cswidth;
}
function MoveOutDiv() {
if (MyMar == null) { MyMar = setInterval(Marquee, speed); }
}