【问题标题】:I want to make a button that rotates itself after click我想制作一个点击后自行旋转的按钮
【发布时间】:2017-07-08 17:17:25
【问题描述】:

我想制作一个点击后会自行旋转的按钮。该按钮有一个动态名称(id、class)。单击它时,它会显示或隐藏 wordpress 帖子的一部分。当帖子展开时,我希望它向上,向下包裹时。

<div>
    <div class="button_wrap" id="opis<?php echo get_the_ID()?>"> <button class="button" type="button" id="button<?php echo get_the_ID()?>"></button> </div>
</div>
<script>
    $("#button<?php echo get_the_ID()?>").click(function() {
        $("#opis<?php echo get_the_ID()?>").slideToggle(1000);
    });
</script>

CSS

.button_wrap {
    width: 100%;
    height: 80px;
    text-align: center;
}
.button {
    background: url(/arrow.svg) no-repeat;
    margin-top: 20px;
    cursor: pointer;
    height: 50px;
    width: 75px;
    border: none;
    transition: .3s ease;
    padding-bottom: 5px;
}
.button:hover {
    background: url(/arrow.svg) no-repeat;
    color: red;
    cursor: pointer;
    height: 50px;
    width: 75px;
    border: none;
    transition: .3s ease;
    margin-top: 13px;
}
.opisy {
    display: none;
}

我相信我必须向按钮添加一个 css 类,但我必须在第二次单击后将其删除。我有点迷茫,也许有更简单的方法?

【问题讨论】:

    标签: javascript css button rotation show-hide


    【解决方案1】:

    最简单的方法。

    $('.btn').click(function(){
      $(this).toggleClass('rotate');
    });
    .btn {
      transition: all 1s ease-in;
    }
    
    .rotate {
      transform: rotate(360deg);
      transition: all 1s ease-in;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button class='btn'>content</button>

    【讨论】:

    • 它确实有效,但我有一个奇怪的错误,即只有其他按钮旋转。
    • @Tad 描述那个错误。
    • 有一个循环显示 wordpress 帖子,在该循环中,我有一个使用帖子 id ( id="button" ) 的 id 来定位按钮和帖子。该按钮显示帖子的隐藏内容。也许有一种方法可以进一步隔离脚本?
    • 我将类更改为上一个函数中使用的 id。现在脚本看起来像这样: $("#button").click(function(){ $(this).toggleClass('rotate'); });感谢您的帮助! :)
    • @Tad 很高兴,祝您有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2011-08-27
    • 2017-12-04
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多