日期:2014-05-18 浏览次数:20429 次
function lr(overHide, scrollUl, leftBtn, rightBtn, num) { var doc = document, overHide = doc.getElementById(overHide), scrollUl = doc.getElementById(scrollUl), liArr = scrollUl.getElementsByTagName('li'), len = liArr.length, leftBtn = doc.getElementById(leftBtn), rightBtn = doc.getElementById(rightBtn), overWidth = overHide.offsetWidth; scrollUl.style.width = len * num + 'px'; var inWidth = scrollUl.offsetWidth; function checkBtn(overHide, scrollUl, leftBtn, rightBtn) {//判断左右按钮显示与隐藏 if (overWidth >= inWidth) {//内容不够不需要左右按钮 leftBtn.style.visibility = 'hidden'; rightBtn.style.visibility = 'hidden'; } else if (overHide.scrollLeft == 0) {//右显示左隐藏 leftBtn.style.visibility = 'hidden'; rightBtn.style.visibility = 'visible'; } else if ((overHide.scrollLeft + overWidth) == inWidth) {//左显示右隐藏 leftBtn.style.visibility = 'visible'; rightBtn.style.visibility = 'hidden'; } else if ((overHide.scrollLeft + overWidth) < inWidth && overHide.scrollLeft > 0) {//左右均显示 leftBtn.style.visibility = 'visible'; rightBtn.style.visibility = 'visible'; } } checkBtn(overHide, scrollUl, leftBtn, rightBtn); function stepL(obj, step) { var n = 0; function scrollL(obj, step) { if (n < step) { obj.scrollLeft += 5; n += 5; } else { clearTimeout(comeon) } checkBtn(overHide, scrollUl, leftBtn, rightBtn) var comeon = setTimeout(function () { scrollL(obj, step) }, 20); } scrollL(obj, step); } function stepR(obj, step) { var n = step; function scrollR(obj, step) { if (n > 0) { obj.scrollLeft -= 5; n -= 5; } else { clearTimeout(comeon) } checkBtn(overHide, scrollUl, leftBtn, rightBtn) var comeon = setTimeout(function () { scrollR(obj, step) }, 20); } scrollR(obj, step); } leftBtn.onclick = function () { stepR(overHide, num) } rightBtn.onclick = function () { stepL(overHide, num) } } lr('scroll_cont', 'scroll_ul', 's_L', 's_R', 122);