I'm struggling to comprehend why this code isn't functioning as expected

There is a need to update the displayed image. The two images are stacked on top of each other, with one having an opacity of 100 (active) and should be positioned above the inactive one based on z-index. The inactive image has an opacity of 0. When one image is clicked, the other should switch to 100 opacity and a higher z-index. (animations are not a priority).

  • The initial oddity: when the page loads, the downward arrow is visible even though it lacks the active class.
  • The second oddity: clicking on the image does not trigger any action.

HTML:

<div id="menu_container">
<table>
<tr>
<td>
<h1>Open Menu</h1>
</td>
<td>
<img src="arrow_up.gif" alt="arrow" class="arrow_up active_img"/> 
<img src="arrow_down.gif" alt="arrow" class="arrow_down"/>
</td>
</tr>
</table>
</div>

CSS for normal and active images:

#menu_container table tr td img {
    width:50px;
    height:50px;
    opacity:0%;
    cursor:pointer;
    display:block;
    position:absolute;
    bottom:0px;
    position:absolute;
    z-index:1;
}
.active_img {
    width:50px;
    height:50px;
    cursor:pointer;
    opacity:100;
    position:absolute;
    z-index:2;
    }

Lastly, the script itself:

var menu = function() {
    $('.arrow_up').click(function() {
        $('#header').animate({top: '10%'}, 300);
        $('#menu_container').animate({top: '15%'},300);

        var active = $('.active_img');

        active.removeClass('active_img');
        active.next().addClass('active_img');

        $('#menu_list').show();
    });
    $('.arrow_down').click(function() {
        $('#header').animate({top: '50%'}, 300);
        $('#menu_container').animate({top: '55%'},300);

        var active = $('.active_img');

        active.removeClass('active_img');
        active.prev().addClass('active_img');

        $('#menu_list').hide();
    });
};
$(document).ready(menu);

Answer №1

Multiple mistakes can be found in the given code.

  1. The value for opacity should range from 0 to 1, not in percentages.
  2. The opacity at .active_img does not override the default opacity due to other selectors with higher specificity in the first rule.
  3. The td within the #menu_container lacks positioning, causing the images to be absolutely positioned relative to the window rather than the td.

  4. The #menu_container is not positioned, resulting in it staying stationary when animating the top property.

var menu = function() {
    $('.arrow_up').click(function() {
        $('#header').animate({top: '10%'}, 300);
        $('#menu_container').animate({top: '15%'},300);

        var active = $('.active_img');

        active.removeClass('active_img');
        active.next().addClass('active_img');

        $('#menu_list').show();
    });
    $('.arrow_down').click(function() {
        $('#header').animate({top: '50%'}, 300);
        $('#menu_container').animate({top: '55%'},300);

        var active = $('.active_img');

        active.removeClass('active_img');
        active.prev().addClass('active_img');

        $('#menu_list').hide();
    });
};
$(document).ready(menu);
#menu_container {
  position:absolute;
}

#menu_container table tr td {
  position:relative;
}

#menu_container table tr td img {
  width:50px;
  height:50px;
  opacity:0;
  cursor:pointer;
  display:block;
  position:absolute;
  bottom:0px;
  z-index:1;
}

#menu_container table tr td .active_img {
  opacity:1;
  z-index:2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="menu_container">
  <table>
    <tr>
      <td>
<h1>Menü megnyitása</h1>
      </td>
      <td>
<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-up-b-128.png" alt="arrow" class="arrow_up active_img"/> 
<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-128.png" alt="arrow" class="arrow_down"/>
      </td>
    </tr>
  </table>
</div>

The unexpected movement of the header downwards may be due to the undefined initial value of top. However, this behavior might be intentional and requires further clarification.

This revision does not aim to resolve any design issues as that falls beyond the current scope of work.

Answer №2

There is a simpler way to accomplish this task.

<div id="clicker">
<img src="picture1.png"/>
<img src="picture2.png" style="display:none"/>
</div>

<script>
$("#clicker").click(function() {
    $(this).find('img').toggle();
});
</script>

Answer №3

If you want your code to function properly, simply eliminate the z-index:1 from the style of #menu_container table tr td img.

#menu_container table tr td img {
width:50px;
height:50px;
opacity:0%;
cursor:pointer;
display:block;
position:absolute;
bottom:0px;
position:absolute;

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript code to always keep the element on top of the page: "Make

I have developed a straightforward chrome extension tool that displays a small text message (a div with z-index 999999) on the webpage that the user is currently viewing. However, I am facing a problem where the div sometimes gets hidden beneath the existi ...

`Center the image on top of the text`

Currently, I am working with Bootstrap 4 to create a component that includes two tiles. Each tile has an icon image on the left side and a hyperlink on the right side. On desktop view, the tiles should be displayed horizontally and vertically centered. How ...

What is the best way to "re-upload" drop-down selection using javascript?

I am attempting to automate a drop-down selection process on a website using a headless browser called Firefox Marionette. The website is not under my control, and the process involves: A primary drop-down menu where I can choose an option. A secondary dr ...

Steps for assigning an id to a newly created div within a parent div while in design mode

Imagine creating a div element with the attribute contenteditable="true", and then checking the console for what happens next: 1st. In the console, you only see a simple div tag. <div id="typbody" contenteditable="true" style="width:100%; height:200px ...

Is there a way for me to adjust the font size across the entire page?

Most CSS classes come with a fixed font-size value already set. For instance: font-size: 16px font-size: 14px etc. Is there a way to increase the font size of the entire page by 110%? For example, font-size: 16px -> 17.6 font-size: 14px -> 15.4 ...

The boundary of embedded components

I am looking for CSS code that will allow me to arrange all elements with specific indents, such as placing the first element on the left side of the page, followed by an image with some indentation, and so on. <div class="container-fluid"> &l ...

Trouble with hiding elements using Ajax when dynamically assigned ids

Currently, I am working on incorporating jQuery into my project to enable/disable functionality using Ajax. The function is working fine, but the result is only displayed after a page refresh. The dynamic condition for the Ajax response is not functioning ...

`The multiple selection options are not appearing correctly on the screen`

Below is the code I am using: <select id="accessList" name="accessList" multiple="multiple" style="position:relative;left:5px;overflow-x:auto;width:200px"> <option value="36453" style="text-align:left;width:auto">TestGroupterminal121 ...

Ensure you click the Google captcha v3 button two times in order to successfully carry out the action

I recently integrated recaptcha v3 into a form and encountered an issue where the submit button had to be clicked twice to function correctly. Upon the first click, the captcha validation is triggered. After the second click, the form will successfully red ...

To activate an underline on text, simply right-click and select "text

All the anchor tags on my website have been styled with text-decoration: none. Additionally, I have added a CSS rule based on a helpful answer: a, a:hover, a:active, a:visited { text-decoration:none; } However, whenever I right-click on a link on my ...

What is the best way to add a gradient effect to my image? (link)

All the details are provided in this JSFiddle. Apologies, I am unable to display the link here. It can be found in the comments section! I'm facing difficulty achieving a red gradient over the image.. ...

Getting rid of the border on a material-ui v4 textbox

I am currently using Material-UI version 4 and have not upgraded to the latest mui v5. I have experimented with various solutions, but so far none have been successful. My goal is simply to eliminate or hide the border around the textfield component. < ...

Sending a form with checkboxes via AJAX

Currently, I am working with Laravel 4 and have a form that includes a list of departments along with their respective members. To organize this information, I decided to implement an accordion feature wherein all members are grouped under a particular dep ...

Tips for troubleshooting grid display issues in Internet Explorer

.container{ display: grid; grid-template-columns: minmax(200px, 7fr) 4.4fr; grid-column-gap: 64px; } .item{ background: red; height: 100px; } <div class="container"> <div class='item item-1'></div> <div class=&a ...

Creating a sleek animation with JQuery for a dynamic-width div

Here is a snippet from my latest project. Take a look at the demonstrationFIDDLE. In this project, I have created a list with buttons: <a href="#f1" class="bt">1 <div class="show">Computers Networking</div> </a> When you ...

"Seamless payment processing and gratitude display in PHP and JavaScript checkout and

I am currently working on integrating a Stripe checkout for my ecommerce website as part of my latest project. Everything seems to be functioning well, but I have a few questions that are causing some confusion. Is it a good idea to use AJAX for the che ...

Prevent unauthorized AJAX requests from external sources within the Node application

I am looking for a way to prevent AJAX requests from being made outside of my application. I need to ensure that the response is not sent when the AJAX request is initiated from external sources, even if the URL is copied and pasted into a browser. My te ...

Gradually disappear when scrolling "not fluid" or "jerky"

I recently developed a jQuery code to create a fade-out effect on an overlay while scrolling down. Everything seemed to be working smoothly until I noticed that on certain older PCs, the fade effect appears choppy and inconsistent. Strangely enough, on my ...

Although Angular allows for CSS to be added in DevTools, it seems to be ineffective when included directly in

Looking to set a maximum length for ellipsis on multiline text using CSS, I tried the following: .body { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; } However, this approach did not work. Interesti ...

Assign a specific attribute to a checkbox, gather all selected attributes, and transmit them using jQuery

I have a system generated table with approximately 800 rows that needs checkboxes added for users to select specific rows for more information. I can add a checkbox column to the entire table at once, but I don't want to manually assign values to each ...