使用scrollTo插件和单击并按住功能将导航元素添加到页面

发布于 2024-11-01 15:09:33 字数 1262 浏览 0 评论 0原文

您可以访问以下网址查看该网站:www.highdwellercreative.com/colife。该网站基于这样的概念:城市将随着公司的发展而发展,我们想要一种不同的感觉,然后使用提供的滚动条进行滚动。我一直在研究由四个方向箭头(上、下、左、右)组成的键盘式导航。

我目前正在使用 setInterval() 函数来循环 mousedown() 上的scrollTo 插件,并使用clearInterval() 函数来结束 mouseup() 上的循环。

我还将重复间隔设置为 1 毫秒,以实现平滑滚动。我已经包含了使我迄今为止所完成的工作有效的代码,但它仍然有点粗糙,尤其是在 Firefox 4 中。到目前为止,这确实适用于所有浏览器,包括 IE,但它可能会更流畅,我在不知道如何实现这一点。此外,如果您单击其中一个导航按钮并立即右键单击它,页面就会卡在该方向上滚动。任何有关如何改进这一点的帮助将不胜感激。

var rightId;

$('span#right').mousedown(function() {

    rightId = setInterval(function(){$.scrollTo('+=5px', {axis:'x'})}, 1);

}).mouseup(function() {

    clearInterval(rightId);

});

var leftId;

$('span#left').mousedown(function() {

    leftId = setInterval(function(){$.scrollTo('-=5px', {axis:'x'})}, 1);

}).mouseup(function() {

    clearInterval(leftId);

});

var upId;

$('span#up').mousedown(function() {

    upId = setInterval(function(){$.scrollTo('-=5px', {axis:'y'})}, 1);

}).mouseup(function() {

    clearInterval(upId);

});

var downId;

$('span#down').mousedown(function() {

    downId = setInterval(function(){$.scrollTo('+=5px', {axis:'y'})}, 1);

}).mouseup(function() {

    clearInterval(downId);

});

You can view the site at www.highdwellercreative.com/colife. This site is based around the concept the the city will grow as the company grows and we wanted a different feeling then scrolling with the scroll bars provided. I have been working on a keypad style of navigation that consists of four directional arrows (up, down, left, right).

I am currently using setInterval() function to loop the scrollTo Plugin on mousedown() and the clearInterval() function to end the loop on mouseup().

I have also set the interval to repeat at 1 millisecond to achieve smooth scrolling. I have included the code that makes what I have accomplished so far work but it is still a bit rough, especially in Firefox 4. So far this does work in all browsers, including IE, but it could stand to be smoother and I am at a loss of how to accomplish this. Also if you click on one of the navigation buttons and immediately do a right click on it, the page gets stuck scrolling in that direction. Any help on how to improve this would be greatly appreciated.

var rightId;

$('span#right').mousedown(function() {

    rightId = setInterval(function(){$.scrollTo('+=5px', {axis:'x'})}, 1);

}).mouseup(function() {

    clearInterval(rightId);

});

var leftId;

$('span#left').mousedown(function() {

    leftId = setInterval(function(){$.scrollTo('-=5px', {axis:'x'})}, 1);

}).mouseup(function() {

    clearInterval(leftId);

});

var upId;

$('span#up').mousedown(function() {

    upId = setInterval(function(){$.scrollTo('-=5px', {axis:'y'})}, 1);

}).mouseup(function() {

    clearInterval(upId);

});

var downId;

$('span#down').mousedown(function() {

    downId = setInterval(function(){$.scrollTo('+=5px', {axis:'y'})}, 1);

}).mouseup(function() {

    clearInterval(downId);

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2024-11-08 15:09:33

您当然可以将这些全部放入一个函数中:

function moveAround(elem,dir,moveAxis) {
  var moving;
  $(elem).mousedown(function() {
    moving = setInterval(function(){$.scrollTo(dir+'=5px', {axis:moveAxis})}, 1);
  }).mouseup(function() {
    clearInterval(moving);
  });
}
moveAround('span#left','-','x');
moveAround('span#right','+','x');
moveAround('span#up','-','y');
moveAround('span#down','+','y');

就功能而言,您可能希望双击将您移动 500 像素或其他。只是为了让人们能够更快地游览城市。 :)

另一种方法是使用 .animate,以及调用相同函数的回调函数(只要 mousedown 仍然处于活动状态)。

You could certainly put these all into a single function:

function moveAround(elem,dir,moveAxis) {
  var moving;
  $(elem).mousedown(function() {
    moving = setInterval(function(){$.scrollTo(dir+'=5px', {axis:moveAxis})}, 1);
  }).mouseup(function() {
    clearInterval(moving);
  });
}
moveAround('span#left','-','x');
moveAround('span#right','+','x');
moveAround('span#up','-','y');
moveAround('span#down','+','y');

Just in terms of functionality, you may want to make a double click move you 500px or something. Just so people can get around the city faster. :)

An alternative way to do this would be to use .animate, with a callback function that called the same function (as long as the mousedown was still active).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文