The size of the div adjusts post sliding action

I have a unique little script that I've included below which is designed to add a series of divs to a sliding panel triggered by a button click.

<script type="text/javascript">
    // Clicking the button initiates the slide for the panel.
    $('#button').click(function(){
        $('.panel').slideToggle('slow');
    });

    // There are 100 specific divs being inserted.
    var divsToPlace = 100;
    for (i = 0; i < divsToPlace; i++) {
        $( '<div/>', {'class': 'addDiv'}).appendTo('#addHere');
    }
</script>

These divs create a puzzling formation: 100 squares arranged in a 10 by 10 grid, with no gaps between them and empty interiors.

.addDiv {
    border: 1px solid #000;
    margin-top: -1px;
    margin-right: -1px;
    float: left;
    width: 10px;
    height: 10px;
}

The desired outcome is a panel containing this grid-like pattern. The insertion of the divs via script is crucial.

However, there's an issue where the bottom border of the panel jumps up after the animation ends, leaving the divs positioned oddly between the panel and the rest of the page.

If you could provide help in resolving this dilemma, it would be greatly appreciated.

The complete code is provided here for ease of testing:

<html>
<head>
  <style type="text/css">
  #header {
    margin: auto;
    width: 100px;
    text-align: center;
  }
  .addDiv {
    border: 1px solid #000;
    margin-top: -1px;
    margin-right: -1px;
    float: left;
    width: 10px;
    height: 10px;
  }
  .panel {
    border: 1px solid #666;
    display: none;
    margin: auto;
    width: 305px;
  }
  </style>
</head>

<body>
  <div id='header'><button id='button'>Panel</button></div>
  <div class='panel'>
    List:
    <ul>
      <li>li 1</li>
      <li>li 2</li>
    </ul>
    <div id='addHere'></div>
  </div>
</body>

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>
<script type="text/javascript">
    $('#button').click(function(){
        $('.panel').slideToggle('slow');
    });

    var divsToPlace = 100;
    for (i = 0; i < divsToPlace; i++) {
        $( '<div/>', {'class': 'addDiv'}).appendTo('#addHere');
    }
</script>
</html>

P.S. If I need to reference one element multiple times, how should I do so?

// Like this?
/* ... code
$('#element').thing();
$('#element').another_thing();
... code ...
$('#element').more_thing();
code... */

// Or like this?
/* ... code
var element = $('#element');
element.thing();
element.another_thing();
... code ...
element.more_thing();
code... */

Answer №1

I discovered two solutions to resolve this particular issue. 1) Make sure to include the float attribute in the parent div (.panel). 2) Eliminate float attributes from .addDiv and opt for display:inline-block instead. Establish a new directive to eradicate the line-heights in #addHere. Take a look at this demonstration. http://jsfiddle.net/QeADw/

.addDiv {
    border: 1px solid #000;
    margin-right: -1px;
    margin-top:-1px;
    width: 10px;
    height: 10px;
    display:inline-block;
}
#addHere{
    line-height:1%;
}

Trust this information proves beneficial.

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

retrieveStyle() rooted framework Category

I have implemented a customized Native Base style theme following the instructions in this link. Imports: import material from './native-base-theme/variables/material'; import getTheme from './native-base-theme/components'; return ( ...

How to easily open a search page with just a click on the search field in CodeIgniter?

I am in the process of implementing a search feature in CodeIgniter. My view file is divided into two main sections: Header section, which includes the search bar <?php echo form_open('controller/live_search');?> <div class="toolba ...

Common pitfalls and glitches when working on WebKit for iOS and Android development

Embarking on the development of a significant touch-enabled mobile WebKit application. In search of any suggestions or recommendations regarding differences from standard desktop-based browsers. For instance: In iOS, click events do not bubble up to ...

Transferring data from Tomcat Listener to a JavaScript function

I've been attempting to send objects from a Java Listener (not the JavaScript eventListener!) to a JavaScript application, but so far, I haven't had any success. The specific requirement is that a JavaScript application running in the browser sh ...

The module in the relative path cannot be located by Node.js

Node.js is giving me some trouble with exporting and importing classes from multiple files. In one file, I have two classes that I'm exporting using the following code: module.exports = {TrigInter, Hilbert}; However, when I try to import these classe ...

Create a custom div class specifically designed for an image button

Apologies if my title is a bit misleading, I struggled to find the right phrasing. Currently, I have images linked to various social networks using href. For instance, clicking on the facebook icon directs to my Facebook page. The layout looks like this: ...

What is the best way to implement multiple ngIf conditions in Angular code?

Is it possible to set multiple conditions in an ngIf statement? I have created a template for multiple users, but there are some content that I don't want certain users to see, such as Super Admin, Admin, Section Users, Division User, and Unit Leader ...

Passing an array from Javascript to a Spring Controller without relying on jQuery

I am struggling to successfully send an array from my JSP page to a Spring Controller. After populating my array with the necessary data in the JSP, I initiate an AJAX request: var ajaxRequest; try{ // Opera 8.0+, Firefox, Safari aj ...

Two events in an arrow-guided grid including a text box (jQuery)

I have successfully implemented an editable table using jQuery where users can click on a cell to input text. The goal now is to enable the use of ARROW keys for quick navigation within the table. Below is my current code: $( function () { $( "td" ). ...

Encountering 'Element not found' error when automating Flight Booking on MakeMyTrip.com using Selenium WebDriver, specifically while trying to select a flight after conducting a search

Currently in the process of automating the flight booking feature on MakeMyTrip.com using Selenium WebDriver. I have implemented separate classes for Page Object Model (POM) and TestNG. The code successfully automates the process up to the point of clicki ...

The equation of jquery plus ie7 results in an undefined value

I am experiencing a strange issue in IE7 with a jQuery script. This problem seems to only occur in IE7. In summary, when I check the console window, it shows that jQuery is not defined - even though I have loaded jQuery (version 1.7.1) from my disk and can ...

Preventing the keyboard from showing on mobile devices when using a React date time picker

I am currently utilizing the React-date-picker component in my ReactJS application. I am encountering an issue where the keyboard is appearing along with the date picker when testing on mobile devices. I have attempted some solutions, but none have resol ...

Achieving functionality with the JQuery.inArray() method

I am currently utilizing inArray to verify if a selected option from a control exists in any of multiple arrays (which is then used to customize and display specific text). You can view my code at http://codepen.io/marklsanders/pen/rWGgPE To check the pr ...

Visual Studio Code encounters a Node.js error stating "Module not found"

I have been struggling to run my Node.js program in VSCode. Despite trying various solutions found on StackOverflow, none of them seem to be working for me. I even attempted the Json file method, but unfortunately, that didn't work either. internal/mo ...

What causes the left click to not trigger in Kendo's Angular Charts?

My homepage features a simple bar chart that displays correctly, but I am having trouble capturing the left click event (the right click works fine). This is an example of code from my template: <kendo-chart *ngIf="(dataExists | async)" [ ...

Ways to center a div within a solo component in Vue.js

.container { display: flex; justify-content: left; } .containertwo { display: flex; justify-content: right; } .center { text-align: center } <div class="container"> //component1 <div class="one">some content</div> </div&g ...

Utilize Multer to seamlessly upload a file in Node.js and Angular environment

I am facing a challenge while trying to upload a file to my SQLite3 database. Every time I attempt the code below, I encounter a 500 error, although there is no indication of such an error in my request. I also experimented with using formData, but unfortu ...

Retrieving data from a child component that has been added in React

One of the challenges I am facing is dealing with a main react component that dynamically appends child components, like <Child />, on button click The structure of my Child component looks something like this: <form> <input .... /> ...

Use serializeArray() in an AJAX request to send special characters

I am currently attempting to use ajax to post an entire form's data. Below is my sample code: $.ajax({ url: "@Url.Action("SaveTPGeneralInfo", "Techpack", new { area = "OMS" })", data: $('#FormId').serializeArray(), ...

Whenever the useState hook is utilized, it will trigger a re-execution

In my code for form validation in the next.js V1, I am experiencing a problem. The code snippet is as follows: setErrorF((errorF) => { if( errorF.FNameAndLName !== "" || errorF.phoneNumber !== "" || errorF.location1 !== ...