简体   繁体   English

单击并显示更多元素

[英]Click and show more element

I want to show more when you click on the div ShowMore but when I do it all section show more and not the element where I just click . 当你点击div ShowMore时我想要显示更多但是当我这样做时所有部分显示更多而不是我点击的元素。

In summary when you click on the div .showMore , div .panel increases in height to show hidden text . 总之,当您单击div .showMore ,div .panel的高度会增加以显示隐藏文本。 But when I click on the .showMore every div with the class .panel also animate while I am only the clicked div animate height 但是当我点击.showMore每个div与类.panel也有动画,而我只是点击div动画高度

I guess that is a small line of code that I miss 我想这是我想念的一小段代码

JS JS

$(".showMore").click(function (){
if ($(".panel").height() == 100) {
    $(this).css({transform:"rotate(90deg)"});
$(".panel").animate({height: "250px"});
}
else if ($(".panel").height() == 250) {
    $(this).css({transform:"rotate(0deg)"});
    $(".panel").animate({height: "100px"});
}
});

Full code here: Codepen 完整代码:Codepen

You just need to make use of $(this) a bit more by accessing it's parent element. 你只需要通过访问它的父元素来更多地使用$(this)

    //alert('ok');
    $(".showMore").click(function (){
    if ($(".panel").height() == 100) {
        $(this).css({transform:"rotate(90deg)"});
    $(this).parent(".panel").animate({height: "250px"});
    }
    else if ($(this > ".panel").height() == 250) {
        $(this).css({transform:"rotate(0deg)"});
        $(this).parent(".panel").animate({height: "100px"});
    }

Find element with class panel that contains clicked element: 使用包含单击元素的类panel查找元素:

$(".showMore").click(function (){

  var panel = $(this).closest(".panel");

  if (panel.height() == 100) {
    $(this).css({transform:"rotate(90deg)"});
    panel.animate({height: "250px"});
  }
  else if (panel.height() == 250) {
    $(this).css({transform:"rotate(0deg)"});
    panel.animate({height: "100px"});
  }
});

Note: get rid of duplicate id s in your html. 注意:摆脱html中的重复id

$(".showMore").click(function (){
    var height=$(this).parent().height();
    if (height == 100) {
        $(this).css({transform:"rotate(90deg)"});
    $(this).parent().animate({height: "250px"});
    }
    else if (height == 250) {
        $(this).css({transform:"rotate(0deg)"});
        $(this).parent().animate({height: "100px"});
    } 
});

http://codepen.io/beshoysemsem/pen/xOQmRQ http://codepen.io/beshoysemsem/pen/xOQmRQ

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

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