简体   繁体   English

jQuery在Clic上旋转跨度

[英]JQuery rotate a span on clic

I'm working on my website (still need work), on the mobile nav, I have an expanding submenu, which has a span with a font character. 我正在网站上工作(仍需要工作),在移动导航上,我有一个扩展子菜单,该子菜单的跨度带有字体字符。 What I want is for this character to rotate 180 degrees when clicked the first tima and go back to zero when clicked again, so far I've only been able to make it ratate the first time I click on it. 我想要的是使该角色在单击第一个tima时可以旋转180度,而在再次单击时可以回到零,到目前为止,我只能在第一次单击它时使其批准。

<ul>
        <li class="parent"><a href="#"><span class="icon-house"></span>Home</a></li>
        <li class="parent"><a href="#"><span class="icon-network"></span>Networks</a></li>
        <li class="parent"><a href="#"><span class="icon-users"></span>Artists</a></li>
        <li class="parent"><a href="#"><span class="icon-pictures"></span>Visual Art</a></li>
        <li class="submenu parent genres">
            <a href="#"><span class="icon-music"></span>Genres<span class="icon-arrow-down6 caret flechitauno"></span></a>
            <ul class="children">
                <li><a href="#">House <span class="icon-music3"></span></a></li>
                <li><a href="#">Trance <span class="icon-music3"></span></a></li>
                <li><a href="#">Drum & Bass <span class="icon-music3"></span></a></li>
            </ul>
        </li>
        <li class="parent"><a href="#"><span class="icon-info"></span>About this site</a></li>      
</ul>

this is the jquery that I've been trying to use: 这是我一直在尝试使用的jQuery:

$(document).ready(main);
var flechitauno = 1;

//flip
$('.genres').click(function(){
    if(flechitauno == 1){
        $('.flechitauno').css({
            transform: 'rotate(180deg)'
        });
        flechitauno = 0;
    }else{
        flechitauno = 1;
        $('flechitauno').css({
            transform: 'ratate(0deg)'
        });
    }
    });
}

This is the adress of the website: http://66.68.113.215/design-3 这是该网站的地址: http : //66.68.113.215/design-3

the js file is /js/menu.js, and the css is /css/menu.css js文件是/js/menu.js,css是/css/menu.css

Try something like this (see effect working here: https://jsfiddle.net/p1xfuc1t/ ): 尝试这样的事情(请参见此处的效果: https : //jsfiddle.net/p1xfuc1t/ ):

HTML: HTML:

<li class="submenu parent genres">
            <a href="#"><span class="icon-music"></span>Genres<span class="icon-arrow-down6 caret flechitauno"></span></a>
            <ul class="children">
                <li><a href="#">House <span class="icon-music3"></span></a>      </li>
                <li><a href="#">Trance <span class="icon-music3"></span></a></li>
                <li><a href="#">Drum & Bass <span class="icon-music3"></span></a></li>
            </ul>

CSS: CSS:

@keyframes roll {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}

JS JS

$(document).ready(function() {

  $('.genres').on('click', function() {
    $('.flechitauno').css(
      "animation", "roll 3s"
    )
  });
});

Aslo check your syntax for the .css() method, it dosent seem right. Aslo检查.css()方法的语法,似乎正确。 Well either this or you use an external library like: http://jqueryrotate.com/ . 好吧,这或者您可以使用一个外部库,例如: http : //jqueryrotate.com/

So, I decided to listen to jsfiddle.net/0oo0fyra/1 – timo, who asked why not use plain javascript, so I researched a little bit more and came up with this solution 因此,我决定听jsfiddle.net/0oo0fyra/1 – timo,他问为什么不使用普通的javascript,所以我进行了更多研究并提出了此解决方案

For the html, I used some in-line js to trigger the function 'flip' in a separate js file, since this a tag was already styled as block with css, I decided to use it to activate the onclick function, sending the span id as an argument to the function 对于html,我使用了一些内联js在单独的js文件中触发函数“ flip”,因为该标签已经被CSS样式化为块,所以我决定使用它来激活oncl​​ick函数,并发送跨度id作为函数的参数

<a href="#" onclick="flip('genres')"><span class="icon-music"></span>Genres<span class="icon-arrow-down6 caret" id="genres"></span></a>

In the js file 'menu.js' I defined a variable to work with the function, and the 'flip' function, I defined the starting value of the variable 'voltear to 1' outside of the function as a global one to that it won't be reassigned every time the onclick function triggers, then the function receives the id of the span and uses it to target the span, then I just wrote what I wanted my span to do 在js文件“ menu.js”中,我定义了一个与该函数一起使用的变量,在“ flip”函数中,我将该函数外部的变量“ voltear to 1”的起始值定义为该函数的全局变量。不会在每次onclick函数触发时重新分配,然后该函数接收跨度的ID并使用它来确定跨度,然后我就写了我希望跨度执行的操作

var voltear = 1
function flip(id){
if (voltear == 1) {
    document.getElementById(id).style.transform = "rotate(180deg)";
    voltear = 0;
}else{
        document.getElementById(id).style.transform = "rotate(0deg)";
        voltear = 1;
    }
}

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

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