jQuery: Toggle rotate div on click function(jQuery:在点击功能上切换旋转 div)
问题描述
我正在尝试确定是否可以在单击功能上切换 div 的旋转,例如单击菜单按钮可将箭头从向下旋转到向上,再次单击将反转(从上到下).
我有一个 jsfiddle 演示设置,它会更有意义:http://jsfiddle.net/bf7Ke/1/
jQuery —
I'm trying to work out if it's possible to toggle the rotation of a div on a click function, e.g. Clicking on a menu button rotates the arrow from pointing downwards to upwards and clicking again will reverse this (upwards to downwards).
I have a jsfiddle demo setup which will make more sense of it: http://jsfiddle.net/bf7Ke/1/
jQuery —
$(document).ready(function(){
$('.menu-category-title').click(function(){
$('#menu-'+$(this).attr('hook')).fadeToggle('slow');
$(this).children('.menu-title-arrow').rotate({animateTo:180});
return false;
});
});
到目前为止,单击 .menu-category-title 会淡入下面的相关内容并将相应的箭头旋转 180 度.再次单击 .menu-category-title 会淡出相关内容,但箭头保持在 180 度.无论如何也可以切换此功能?
我无法弄清楚,任何建议将不胜感激!
Thus far clicking on .menu-category-title fades in the relevant content below and rotates the corresponding arrow by 180 degrees. Clicking .menu-category-title again fades out the relevant content but the arrow stays at 180 degrees. Is there anyway to toggle this function also?
I can't figure it out, any suggestions would be greatly appreciated!
推荐答案
认为你应该在箭头旋转之前检查元素是否可见
Think you should check if element is visible or not before arrow rotation
$(document).ready(function(){
$('.menu-category-title').click(function(){
var elem = $('#menu-'+$(this).attr('hook')),
arrow = $(this).children('.menu-title-arrow')
if (!elem.is(':visible')) {
arrow.rotate({animateTo:180});
} else {
arrow.rotate({animateTo:360});
}
elem.fadeToggle('slow', function() {
});
return false;
});
});
http://jsfiddle.net/bf7Ke/2/
这篇关于jQuery:在点击功能上切换旋转 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jQuery:在点击功能上切换旋转 div
基础教程推荐
- 检查 HTML5 拖放文件类型 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
