I have some troubles with rotating an image when it's clicked.
I've got a menu item with a submenu, but the submenu only shows when the menu item is clicked. Now I have added an image (an arrow) to the menu item to show wether the submenu is opened or closed, so I would like to turn the image upside down if the submenu is opened.
The strange thing is that the following code is the exact code I use in my website, but here it works and on my website the whole dropdown div disappears as soon as I inserted the jQuery code in my file...
$("#arrowed").toggle(function() {
$(".dropdown-content").slideDown(200);
$("#dropdown-image").css({
"-webkit-transform": "rotate(180deg)",
"-moz-transform": "rotate(180deg)",
"transform": "rotate(180deg)"
});
}, function() {
$(".dropdown-content").slideUp(200);
$("#dropdown-image").css({
"-webkit-transform": "rotate(0deg)",
"-moz-transform": "rotate(0deg)",
"transform": "rotate(0deg)"
});
});
* {
margin: 0px;
padding: 0px;
line-height: 100%;
box-sizing: border-box;
border: 0px solid black;
font-family: georgia;
}
.menu {
float: left;
height: 70px;
color: rgba(0,0,0,1.0);
font-size: 20px;
padding: 30px;
text-decoration: none;
}
.header p:hover {
cursor: pointer;
}
.menu:hover, .dropdown:hover .menu {
border-bottom: 5px solid rgba(0,0,0,1.0);
cursor: pointer;
}
#dropdown-image {
width: 70px;
height: 70px;
padding: 15px;
float: right;
position: relative;
top: -30px;
}
#arrowed {
padding-right: 0px;
}
.dropdown {
position: relative;
display: inline-block;
float: left;
}
.dropdown-content {
display: none;
position: absolute;
top: 70px;
width: 150%;
}
.dropdown-inner:link, .dropdown-inner:visited {
display: block;
font-size: 18px;
padding: 16px 20px;
color: rgba(0,0,0,1.0);
background-color: rgba(0,100,0,0.8);
text-decoration: none;
}
.dropdown-inner:hover, .dropdown-inner:active {
display: block;
font-size: 18px;
padding: 16px 20px;
color: rgba(0,0,0,1.0);
background-color: rgba(100,100,100,1.0);
text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="dropdown">
<a class="menu" id="arrowed" href="">Home
<img src="https://cdn1.iconfinder.com/data/icons/pixel-perfect-at-16px-volume-2/16/5001-512.png" id="dropdown-image" />
</a>
<div class="dropdown-content">
<a class="dropdown-inner" href="homepage.php">Homepage</a>
<a class="dropdown-inner" href="item1.php">Item 1</a>
<a class="dropdown-inner" href="item2.php">Item 2</a>
<a class="dropdown-inner" href="item3.php">Item 3</a>
<a class="dropdown-inner" href="item4.php">Item 4</a>
<a class="dropdown-inner" href="item5.php">Item 5</a>
</div>
</div>
So I don't believe the problem is about the jquery, but I can't find the real problem...
Edit: No this is not a duplicate, because in my case, in my js-file the code waits until the page is loaded, I only didn't copy that part of the code, because I didn't think it was relevant.
Update:
So I've gotten closer to an answer, now I use this jQuery code:
$(document).ready(function(){
$("#arrowed").click(function(){
$(".dropdown-content").slideToggle(200);
$("#dropdown-image").css({
"-webkit-transform": "rotate(180deg)",
"-moz-transform": "rotate(180deg)",
"transform": "rotate(180deg)"
});
});
});
The arrow turns upside-down when I open the dropdown, can anyone explain how to turn it back when I close the dropdown? Thanks already!