简体   繁体   English

FontAwesome5(带有JS的SVG)和Jquery将图标旋转(动画)180度

[英]FontAwesome5 (SVG with JS) and Jquery to rotate (animate) an icon 180 degrees

Looking for a bit of help here using FA5 (SVG w/ JS) and Jquery to animate an icon so it will rotate 180 on the click of an element. 在这里使用FA5(带有JS的SVG)和Jquery为图标设置动画,以便在单击元素时旋转180度,以寻求帮助。

My HTML looks like this: 我的HTML看起来像这样:

        <div class="card-block">
            <a id="student-change" data-toggle="collapse" href="#classBreakdownPanel" role="button" aria-expanded="false" aria-controls="classBreakdownPanel">
                <div class="row">
                    <div class="col">
                        <span class="card-text text-primary">View details</p>
                    </div>
                    <div class="col">
                        <i id="blueCaret" class="fas fa-2x blue-caret fa-caret-circle-up float-right"></i>
                    </div>
                </div>
            </a>
        </div>

When rendered in browser it becomes this: 在浏览器中呈现时,它变为:

        <div class="card-block">
            <a id="student-change" data-toggle="collapse" href="#classBreakdownPanel" role="button" aria-expanded="false" aria-controls="classBreakdownPanel" class="collapsed">
                <div class="row">
                    <div class="col">
                        <span class="card-text text-primary">View details<p></p>
                    </span></div>
                    <div class="col">
                        <svg id="blueCaret" class="svg-inline--fa fa-caret-circle-up fa-w-16 fa-2x blue-caret float-right" aria-hidden="true" data-prefix="fas" data-icon="caret-circle-up" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M8 256C8 119 119 8 256 8s248 111 248 248-111 248-248 248S8 393 8 256zm379.5 27.5l-123-123c-4.7-4.7-12.3-4.7-17 0l-123 123c-7.6 7.6-2.2 20.5 8.5 20.5h246c10.7 0 16.1-12.9 8.5-20.5z"></path></svg><!-- <i id="blueCaret" class="fas fa-2x blue-caret fa-caret-circle-up float-right"></i> -->
                    </div>
                </div>
            </a>
        </div>

CSS looks like this: CSS看起来像这样:

#blueCaret{
    transition-duration: 0.8s;
    transition-property: transform;
}
.rotated{
    -moz-transform:rotate(180deg);
    -webkit-transform:rotate(180deg);
    transform:rotate(180deg);
}

and some very simple jquery: 和一些非常简单的jQuery:

    $('#student-change').click(function(){
        $('#blueCaret').toggleClass("rotated");
        console.log('it should turn');
    });

This works in that the icon goes from pointing up on load, to down when the div below it is expanded on click, and then points up again when the user clicks it again. 这样做的原理是,图标在单击时会从加载时的指向上方变为指向下方的div时指向下方,然后在用户再次单击时再次指向上方。

I would like it to nicely rotate to position however. 我希望它旋转到正确的位置。

Right now it literally just switches from up to down instantly - but I'd like to animate the transition so it looks nice and fluid by rotating it. 现在,它实际上只是立即从上到下切换-但我想为过渡设置动画,以便通过旋转它看起来既美观又流畅。

I'm sure I am missing something simple, but I can't seem to find how to do this. 我敢肯定我缺少一些简单的东西,但是我似乎找不到如何做到这一点。

I believe this may be due to you trying to apply classes to <svg> elements which I believe jQuery can't do. 我认为这可能是由于您尝试将类应用于<svg>元素而我认为jQuery无法做到的。 See this answer to: jQuery SVG, why can't I addClass? 看到以下答案: jQuery SVG,为什么我不能添加类? . One way to fix that is to wrap the <svg> in a containing element and transform that instead. 解决此问题的一种方法是将<svg>包装在一个包含元素中,然后对其进行转换。 Consider this example using the code that you say is rendered in the browser, you may need to run the snippet in fullscreen: 考虑使用您说的在浏览器中呈现的代码的此示例,您可能需要全屏运行代码段:

 $('#student-change').click(function(e) { $('#blueCaret').addClass("rotated"); console.log('it should turn'); return false; }); 
 #blueCaret { display: inline-block; transition: transform 200ms ease; } .rotated { -moz-transform: rotate(180deg); -webkit-transform: rotate(180deg); transform: rotate(180deg); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" /> <div class="card-block"> <a id="student-change" data-toggle="collapse" href="#classBreakdownPanel" role="button" aria-expanded="false" aria-controls="classBreakdownPanel" class="collapsed"> <div class="row"> <div class="col"> <span class="card-text text-primary">View details<p></p> </span></div> <div class="col"> <div id="blueCaret"> <svg class="svg-inline--fa fa-caret-circle-up fa-w-16 fa-2x blue-caret float-right" aria-hidden="true" data-prefix="fas" data-icon="caret-circle-up" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M8 256C8 119 119 8 256 8s248 111 248 248-111 248-248 248S8 393 8 256zm379.5 27.5l-123-123c-4.7-4.7-12.3-4.7-17 0l-123 123c-7.6 7.6-2.2 20.5 8.5 20.5h246c10.7 0 16.1-12.9 8.5-20.5z"></path></svg></div> </div> </div> </a> </div> 

 setTimeout(() =>{ document.querySelector('i') .classList.add('rotate') }, 1000) 
 .rotate { transform: rotate(180deg); transition: transform 1s; } 
 <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" /> <i class="fa fa-caret-up" /> 

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

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