使用键盘控制 jQuery Easing

发布于 2024-12-20 12:40:13 字数 2153 浏览 2 评论 0原文

我在一个非常简单的网站上安装并运行了jquery的缓动插件,可以在这里看到: http://harrisonfjord.com/folio /

我的锚链接代码(调用 jquery 函数将窗口滑动到按下的 div 上)是:

<a href="#" style="" onclick="Animate2id('#c1'); return false" rel="Greetings"><img src="img/btn1.jpg" /></a>
<a href="#" onmouseover="" onclick="Animate2id('#c2'); return false" rel="About Us"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c3'); return false" rel="Plants Plus"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c4'); return false" rel="Kia Cadenza"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c5'); return false" rel="Panadol"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c6'); return false" rel="Asics"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c7'); return false" rel="Tooheys"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c8'); return false" rel="Channel 7 Olympic Coverage"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c9'); return false" rel="Hit us up!"><img src="img/btn1.jpg" /></a>
<span style="font-size:25px" id="display"></span>

javascript 代码如下:

$(window).keyup(function(e){
    if(e.keyCode == 39){
         //magical code goes here
    }
    return false;
});

function Animate2id(id,ease){ //the id to animate, the easing type
    var currentPage=id;
    var animSpeed=2000; //set animation speed
    var $container=$("#container"); //define the container to move
    if(ease){ //check if ease variable is set
        var easeType=ease;
    } else {
        var easeType="easeOutQuart"; //set default easing type
    }
    //do the animation
    $container.stop().animate({"left": -($(id).position().left)}, animSpeed, easeType);
}

我尝试为Animate2id 函数获取您正在查看的当前 div,然后可以使用该函数转到下一个或上一个 div(但甚至不知道我是否正确完成了...)。

显然对 jquery/javascript 还很陌生,所以非常感谢任何帮助。

I have jquery's easing plugin up and running on a very simple website, can be seen here: http://harrisonfjord.com/folio/

The code I have for anchor links (which call the jquery function to slide the window over to whichever div is pressed) is:

<a href="#" style="" onclick="Animate2id('#c1'); return false" rel="Greetings"><img src="img/btn1.jpg" /></a>
<a href="#" onmouseover="" onclick="Animate2id('#c2'); return false" rel="About Us"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c3'); return false" rel="Plants Plus"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c4'); return false" rel="Kia Cadenza"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c5'); return false" rel="Panadol"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c6'); return false" rel="Asics"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c7'); return false" rel="Tooheys"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c8'); return false" rel="Channel 7 Olympic Coverage"><img src="img/btn1.jpg" /></a>
<a href="#" onclick="Animate2id('#c9'); return false" rel="Hit us up!"><img src="img/btn1.jpg" /></a>
<span style="font-size:25px" id="display"></span>

And the javascript code is as follows:

$(window).keyup(function(e){
    if(e.keyCode == 39){
         //magical code goes here
    }
    return false;
});

function Animate2id(id,ease){ //the id to animate, the easing type
    var currentPage=id;
    var animSpeed=2000; //set animation speed
    var $container=$("#container"); //define the container to move
    if(ease){ //check if ease variable is set
        var easeType=ease;
    } else {
        var easeType="easeOutQuart"; //set default easing type
    }
    //do the animation
    $container.stop().animate({"left": -($(id).position().left)}, animSpeed, easeType);
}

I've tried setting up a variable for the Animate2id function that gets the current div that you're looking at, which could then be used to go to the next or previous div (but don't even know if I've done that correctly...).

Obviously still new to jquery/javascript so would very much appreciate any help.

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

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

发布评论

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

评论(1

泡沫很甜 2024-12-27 12:40:13

首先,我会修改你的 HTML,点击处理程序应该放在 JavaScript 中。使用 href 属性指向相关的 div:

<div id="link-container">
    <a href="#c1" style="" rel="Greetings"><img src="img/btn1.jpg" /></a>
    <a href="#c2" rel="About Us"><img src="img/btn1.jpg" /></a>
    <a href="#c3" rel="Plants Plus"><img src="img/btn1.jpg" /></a>
    <a href="#c4" rel="Kia Cadenza"><img src="img/btn1.jpg" /></a>
    <a href="#c5" rel="Panadol"><img src="img/btn1.jpg" /></a>
    <a href="#c6" rel="Asics"><img src="img/btn1.jpg" /></a>
    <a href="#c7" rel="Tooheys"><img src="img/btn1.jpg" /></a>
    <a href="#c8" rel="Channel 7 Olympic Coverage"><img src="img/btn1.jpg" /></a>
    <a href="#c9" rel="Hit us up!"><img src="img/btn1.jpg" /></a>
    <span style="font-size:25px" id="display"></span>
</div>

然后在 jQuery 中,绑定所有处理程序:

$("#link-container A").click(function(e) {
    e.preventDefault();
    Animate2id($(this).attr("href"));   
});

然后在 Animate2id 中在当前 div 上添加一个类>:

function Animate2id(id, ease){ //the id to animate, the easing type
    $(".current").removeClass("current");
    $(id).addClass("current");

    // rest of this functions' code ...
}

现在类位于选定的 div 上,您可以计算出按左/右光标时需要进入的方向:

$(window).keyup(function(e){
    if (e.which == 37) { // left cursor
         var $prevDiv = $(".current").prev("div");
         Animate2id($prevDiv.attr("id"))    
    }

    if (e.which == 39) { // right cursor
         var $nextDiv = $(".current").next("div");
         Animate2id($nextDiv.attr("id"))    
    }
});

First of all, I'd amend your HTML, click handlers should be put in javascript. Use the href attribute to point to your related div:

<div id="link-container">
    <a href="#c1" style="" rel="Greetings"><img src="img/btn1.jpg" /></a>
    <a href="#c2" rel="About Us"><img src="img/btn1.jpg" /></a>
    <a href="#c3" rel="Plants Plus"><img src="img/btn1.jpg" /></a>
    <a href="#c4" rel="Kia Cadenza"><img src="img/btn1.jpg" /></a>
    <a href="#c5" rel="Panadol"><img src="img/btn1.jpg" /></a>
    <a href="#c6" rel="Asics"><img src="img/btn1.jpg" /></a>
    <a href="#c7" rel="Tooheys"><img src="img/btn1.jpg" /></a>
    <a href="#c8" rel="Channel 7 Olympic Coverage"><img src="img/btn1.jpg" /></a>
    <a href="#c9" rel="Hit us up!"><img src="img/btn1.jpg" /></a>
    <span style="font-size:25px" id="display"></span>
</div>

Then in jQuery, bind all the handlers:

$("#link-container A").click(function(e) {
    e.preventDefault();
    Animate2id($(this).attr("href"));   
});

Then in Animate2id add a class on the current div:

function Animate2id(id, ease){ //the id to animate, the easing type
    $(".current").removeClass("current");
    $(id).addClass("current");

    // rest of this functions' code ...
}

With the class now on the selected div you can work out which direction you need to go in on left/right cursor press:

$(window).keyup(function(e){
    if (e.which == 37) { // left cursor
         var $prevDiv = $(".current").prev("div");
         Animate2id($prevDiv.attr("id"))    
    }

    if (e.which == 39) { // right cursor
         var $nextDiv = $(".current").next("div");
         Animate2id($nextDiv.attr("id"))    
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文