简体   繁体   English

点击(功能)不起作用

[英]click(function) doesn't work

I tried to do some animation with click function button and left/right arrow keyboard, just a simple left-right sliding animation. 我试图用单击功能按钮和左/右箭头键盘做一些动画,只是一个简单的左右滑动动画。 At first those script works perfectly, til I edit here and there, but not these JS. 最初,这些脚本可以完美运行,直到我在这里和那里进行编辑为止,但对这些JS却不是。

When I run it again, only the keyboard based animation works, the button click animation wont work anymore. 当我再次运行它时,仅基于键盘的动画有效,按钮单击动画不再有效。

 $(document).ready(function(){ $('.btnright').click(function(){ $('.project').animate({ left: '-=150px', }); }); $('.btnleft').click(function(){ $('.project').animate({ left: '+=150px', }); }); }); 

I do test the the click function with alert, but the alert wont work too. 我确实使用警报测试了click功能,但是警报也无法正常工作。

 $(document).ready(function(){ $('.btnright').click(function(){ alert('right'); }); $('.btnleft').click(function(){ alert('left'); }); }); 

And I already check if I used the wrong class, but now, it's just allright. 而且我已经检查了我是否使用了错误的类,但是现在,一切都很好。

<div class="btnright"><span class="material-icons">keyboard_arrow_right</span></div>
    <div class="btnleft"><span class="material-icons">keyboard_arrow_left</span></div>

Here are the keyboard animation script, this keyboard script works fine 这是键盘动画脚本,此键盘脚本可以正常工作

 $(document).on("keydown", function(event) { var x = event.keyCode; if (x == 37) { // left $('.project').animate({ left: '+=150px', }); } else if (x == 39) { // right $('.project').animate({ left: '-=150px', }); } }); 

Maybe I'm missing something. 也许我想念一些东西。 I'll appreciate for ya'll help. 我会感谢您的帮助。 Thx. 谢谢。

Remove the trailing comma from your options object. 从选项对象中删除结尾的逗号。 Also, you need to specify the duration for the animation. 另外,您需要指定动画的持续时间。 Also, .project would need to be absolutely or relatively positioned (instead of default of static) for left to have an effect. 此外, .project将需要是绝对或相对定位(而不是静态默认值) left有效果。 So: 所以:

$('.btnright').click(function(){
    $('.project').animate({
        left: '-=150px'
    }, 500);
});

With CSS: 使用CSS:

.project {
  position: relative;
}

Instead of: 代替:

$('.btnright').click(function(){
    $('.project').animate({
        left: '-=150px',
    });
});

Codepen: http://codepen.io/anon/pen/WQzazR Codepen: http ://codepen.io/anon/pen/WQzazR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM