简体   繁体   English

jQuery单击功能问题

[英]Jquery click function problems

New to jQuery I am trying to have an image that when I click on it I want to move up a certain amount of pixels then when another image is clicked go back down to the original state? jQuery的新手,我试图提供一个图像,当我单击它时,我想向上移动一定数量的像素,然后单击另一个图像时,又回到原始状态?

so far i have tried this 到目前为止,我已经尝试过

$(".wrap").click(function(){
  $(this).css("margin-top", "-15px");
});

I just cant figure out how to make it move down to its original place thanks 我只是想不出如何使它向下移动到原来的位置

Place it in document.ready and use latest JQuery source file: 将其放在document.ready并使用最新的JQuery源文件:

<script type="text/javascript>
$(document).ready(function(){

$(".wrap").on('click',function(){
  $(this).css("margin-top", "-15px");
  });
});

</script>

Try to do: 试着做:

$("img.wrap").click(function(){
    $("img.wrap").css("margin-top", "0px");
    $(this).css("margin-top", "-15px");
});

Try to wrap it in ready() 尝试将其包装在ready()中

And css() may not apply margins so use javascript marginTop like, 而且css()可能不会应用margins因此请使用javascript marginTop类的方法,

$(function(){
    $(".wrap").click(function(){
        this.style.marginTop='-15px';
    });
});

Live Demo 现场演示

For better look use Jquery animate 为了更好的外观,请使用Jani动画

$( ".wrap" ).click(function() {
$(this).animate({
 top: "15px",
}, 500);
                              });

As per my understanding of your question. 根据我对您问题的理解。 I've created a fiddle to help you find a solution. 我创建了一个小提琴来帮助您找到解决方案。 Please have a look at the fiddle and the code snippet below. 请查看下面的小提琴和代码段。 Appreciate your time. 感谢您的时间。

$('.sample img').bind('click',function(){
    $('.sample img').removeAttr('style');
    $(this).css('top',-5);    
});

Do you want like this: 您要这样吗?

var mt = $('.wrap').css('margin-top');
$(".wrap").click(function(){
  if($(this).css('margin-top') == '-15px'){
   $(this).css('margin-top',mt);
  } else {
   $(this).css("margin-top", "-15px");
  }
});

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

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