猿问

使用鼠标悬停从左到右、从右到左滚动

我尝试使用 jQuery (jquery-1.7.1.min.js) 滚动,mouseover但无法滚动。下面是我的代码。

var ele = $('html,body');

var speed = 1,

  scroll = 3,

  scrolling;


$('#up').mouseenter(function() {

  //scroll the element up

  scrolling = window.setInterval(function() {

    ele.scrollTop(ele.scrollTop() - scroll);

  }, speed);

});


$('#down').mouseenter(function() {

  //scroll the element down

  scrolling = window.setInterval(function() {

    ele.scrollTop(ele.scrollTop() + scroll);

  }, speed);

});


$('#up, #down').bind({

  click: function(e) {

    //stop scrolling

    if (scrolling) {

      //prevents the default click action

      e.preventDefault();

    }

  },

  mouseleave: function() {

    if (scrolling) {

      window.clearInterval(scrolling);

      scrolling = false;

    }

  }

});

.control {

  width: 100%;

  position: fixed;

  text-align: center

}


#up.control {

  position: fixed;

  top: 0

}


#down.control {

  position: fixed;

  top: 20

}



/* no needed: */


#scroll {

  overflow-x: scroll;

  width: 500px;

  white-space: nowrap;

  overflow: hidden!imprtant;

}

onmouse左侧和右侧需要从左到右和从左到右滚动文本,但它无法使用 jQuery。



犯罪嫌疑人X
浏览 94回答 0
0回答

智慧大石

您的代码中几乎没有问题:ele = $('html,body');应该是您打算滚动的元素,而不是文档<body>或<html>,例如在您的情况下是<div id="scroll">...你必须使用.scrollLeft()不是scrollTop()因为你是左右滚动,而不是上下滚动。我相信这就是你想要的var ele = $('#scroll');var speed = 10,  scroll = 3,  scrolling; $('#up').mouseenter(function() {  //scroll the element up  scrolling = window.setInterval(function() {    scroll += 0.5;    ele.scrollLeft(ele.scrollLeft() - scroll);  }, speed);  }).mouseleave(function(){  window.clearInterval(scrolling);  scroll = 3;})$('#down').mouseenter(function() {  //scroll the element down  scrolling = window.setInterval(function() {    scroll += 0.5;    ele.scrollLeft(ele.scrollLeft() + scroll);  }, speed);}).mouseleave(function(){  window.clearInterval(scrolling);  scroll = 3;}).control {  width: 100%;  position: fixed;  text-align: center}#up.control {  position: fixed;  top: 0}#down.control {  position: fixed;  top: 20}/* no needed: */#scroll {  overflow-x: scroll;  width: 500px;  white-space: nowrap;  overflow: hidden!imprtant;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script><div id="scroll">  Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content  here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here  and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and  more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more  content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content  here</div><a href="#" id="up" class="control">left</a><a href="#" id="down" class="control">right</a>
随时随地看视频慕课网APP

相关分类

Html5
我要回答