Clicking with jQuery to float left and then bringing back

I have written this JavaScript code that is supposed to float the address div to the center when you click on the info panel using the "addressmoved" class.

Challenges

However, when I click on the trigger button, instead of the address div moving to the center, it goes down to the bottom of the following content, which is not the desired behavior. Additionally, when closing the info panel by clicking the trigger button again, the address div should return to its original position.

Query

How can I ensure that clicking the trigger button moves the address to the center as intended and returns it to the correct position when clicked again? JQuery

<script type="text/javascript">
$(document).ready(function(){
    $(".trigger").click(function(){
        $('.address').removeClass('address').addClass('addressmoved');
        $(".panel").toggle("fast");
        $(this).toggleClass("active");
        return false;
    });
});
</script>

CSS

.addressmoved{
float:left;
right:0;
}

.address{
font-size: 18px;
height: 120px;
position: absolute;
right: 100px;
top: 50px;
width: 200px;
}

HTML

<div class="address">
  <img src="image.png" alt="mainicon" />
  <span>hello</br>
    <em>world</em> 
  </span>
  <ul>
    <li class="icon"> 
      <img class="social" src="icon1.png " alt="icon1"/>    
      </a>      
    </li> 
  </ul>
</div>

Answer №1

Is this the solution you're looking for? DEMO

Below is the HTML code:

<div class="container">
    <button id="trigger" class="button">Info panel</button>
    <div id="info-panel"></div>
</div>

The CSS

body, div, button {
    margin: 0;
    padding: 0;
}
button:focus {
    none;
}
body {
    background: #333;
    font: 1.2em Arial, sans-serif;
}
.container {
}
.button {
    color: white;
    font: inherit;
    border-top-left-radius: 30px;
    border-bottom-left-radius: 30px;
    border: 1px solid gray;
    background: #555;
    margin: 2em 0;
    padding: 1em;
    position: fixed;
    right: 0;
    cursor: pointer;
}
.button:hover {
    padding-right: 1.5em;
}
#info-panel {
    float: right;
    width: 300px;
    height: 400px;
    background: rgba(0, 0, 0, .5);
    margin: .75em 0;
}

Here is the JavaScript code:

$("#trigger").click(function () {
    $("#info-panel").toggle("slow");
});

I implemented the toggle method which can either show or hide the matched element/s when clicked.

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

Unable to retrieve xmlhttp response from local server

When attempting to retrieve an xmlhttp request from my server, which is running on a local machine, function makeRequest(url) { var url = "http://localhost:8081/data/json"; request = new XMLHttpRequest(); request.onreadystatechange = alert ...

Using Jquery for a synchronous "Ajax" request doesn't seem to be functioning

As a newcomer to javascript, I am currently in the process of developing a react/flux app and utilizing jquery for synchronous "ajax" calls (sjax?) to the backend. However, I am encountering inconsistent behavior which seems to be caused by my ajax call no ...

What could be causing the absolute positioned element to not follow the positioning rules as expected?

Currently, I am following a guide on creating a website using Dreamweaver (). However, I have encountered a step in the tutorial that I am struggling to comprehend. The issue lies with an element named #navlink, which is absolutely positioned in the DOM s ...

AngularJS and the Angular UI Router were combined for the first time in

Hey everyone, I could really use some help as I'm just diving into the world of AngularJS. Can anyone guide me on how to tackle these pesky errors? Error: app.js:6 > Uncaught ReferenceError: angular is not defined Error: angular.min.js:6 > Unc ...

"Error message when iterating through JSON using jQuery: 'a' is

I've come across similar discussions on this topic, but I haven't found a solution for looping through an array of key-value pairs in the following format: { "quotes": [ { "author": "Author One", "quote": "Quote One" }, { ...

Extracting Tailwind color palette through API request

After conducting some research, I have not been able to find a definitive answer to my query. I possess a CMS API that supplies branding colors and other assets for a React application that can dynamically change its appearance using a combination of colo ...

How can I make an AJAX request in Rails 3 to retrieve an HTML page?

I am experimenting with using Rails to display an .html.erb template via an ajax request under specific conditions. By default, I am consistently receiving the .js.erb file when making an ajax request. Without delving into the reasons behind this choice, ...

Acquiring information from a variable via an HTTP request

I am new to making http requests and using PHP. I have a code snippet that makes an AJAX call: xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var doc = xmlhttp.response; myFunc( ...

Choosing one item from an array to showcase in a view [Angular]

I've previously inquired about this issue without success. My current challenge involves a store app created with AngularJS. I am trying to implement a feature where clicking on an item will open it in a separate "item" view, displaying only the info ...

Unable to Display Bootstrap 4 Navbar on Hamburger Menu

I am having an issue with my mobile-sized site. Whenever I click the hamburger menu, the Navbar does not appear. I have double-checked and my data-target matches the nav id, so I am not sure why it is not working. <section id="nav"> <na ...

I am having trouble properly positioning an icon next to its corresponding text within a menu item

I'm a newcomer to the world of HTML5 + CSS3 and I'm having trouble with aligning menus, text, and icons within the menu. Here's the issue: Each line of the menu consists of an icon and a text description. The problem is that they are too clo ...

Can loading Ajax from a local file help speed up page load times?

Currently, I am exploring DataTables with the help of this example: https://datatables.net/examples/ajax/simple.html. In this scenario, Ajax is utilized to populate the grid with data retrieved from a large text file. I am curious, when accessing a stati ...

Tips for adjusting the property of an object that has been added to an array?

I have created an object. { "heading": [{ "sections": [] }] } var obj = jQuery.parseJSON('{"header":[{"items":[]}]}'); Then I add elements to the sections var align = jQuery.parseJSON('{"align":""}'); obj["he ...

Ways to match a string against a numeric value

I have a span id with textContent which have their own time(hour/minutes) <div class="here"> <span class="time" >8 min</span> </div> <div class="here"> <span class="time" >22 min&l ...

Remove a row from a table using the hyperlink reference

I am facing an issue where I want to delete a row in the table along with the corresponding database entry by clicking on a href link, but it doesn't work as expected: <table cellpadding="0" cellspacing="0" border="0" class="display" id="example"& ...

What is the functionality of this pseudo element?

I am interested in understanding the purpose of the pseudo element and how it affects the layout without altering the existing markup. Check out the code demo div{ border: 1px solid #000; height: 300px; } div:before{ content: ""; ...

Show a bootstrap modal following the refreshing of the page by using ajax success and setTimeout functions

After making a jQuery ajax request, I want to show a bootstrap modal upon page refresh in the success function. However, it seems like the setTimeout function is being erased once the page reloads, preventing the modal from appearing. I need the content to ...

Dealing with repeated parameters in a URLHow can you handle duplicate

My Ajax select input dynamically changes the URL without refreshing the page. However, I have encountered an issue where repeated parameters stack in the URL when the select input is changed multiple times: [domain]/find.php?cat=1#pricemin=10&pricem ...

The isset function in PHP fails to operate effectively on websites utilizing AJAX

Here is my header.php page that contains links to some pages. I am using AJAX to develop my site. <li><a href="log.php" class="ajaxtrigger">Login</a></li> <li><a href="change_pass.php" class="ajaxtrigger" >Change Passwo ...

Could you please advise me on where I should apply line-height in order for it to work properly?

My div is structured like this: https://i.sstatic.net/3fUNs.png I am trying to decrease the spacing between the lines. I attempted adding a line-height attribute to both the parent div and one of the label elements like this: <div class="row" ...