Click once to reveal divs opening in opposite directions

Is there a way to achieve this JQuery effect efficiently? A helpful user on Stackoverflow assisted me in reaching the desired effect. Now, I want to implement it using JQuery in the most optimal manner possible. For a clearer understanding of what I aim to accomplish, please refer to the following fiddle link:

http://jsfiddle.net/itsbc/U3Lg9/11/

The current implementation is a bit choppy, and I am seeking a smoother transition similar to this example:

http://jsfiddle.net/itsbc/QchfJ/

Thank you in advance, Bc.

Answer №1

Some users in the comments section have recommended sticking with your current setup for performance reasons, especially if you are only using slide animations.

However, if you are looking to achieve a combination of

slideDown animation + hide effect
, you can follow these steps:

CSS:

#hider1 { 
   position:fixed; /* Updated style*/
   top: 0px;       
   background: black;
   width:100%;
   height:48%;
   min-height:0px;
   text-align:center;
   color:white; 
}

#hider2 { 
   background-color:black;
   width:100%;
   height:50%;
   text-align:center;
   position:fixed;  /* Updated style*/
   bottom:0;        /* Updated style*/
   color:white; 
}

JS:

$(function () {
    $('#test').on('click', function () {
        $('#hider1').animate({            
            height: 0,
            opacity: 0.2 //<-- Fancy stuff
         },1000 );

        $('#hider2').animate({            
            height: 0,
            opacity: 0.2 //<-- Fancy stuff
         },1000 );

    });
});


DEMO -> Tested and working in FF and Chrome browsers.

Answer №2

Alright, I made some adjustments to your code for better performance. The jQuery function call has been simplified.

$("#opener").click(function() {
    myApp.websiteOpener.displayWebsite('hider1', 'hider2');
});

After that, you will need a separate file with the necessary code.

var myApp = {};
myApp.websiteOpener = {

    up: null,
    down: null,
    topPanel: null,
    bottomPanel: null,

    displayWebsite: function(tPanel, bPanel) {
        if (typeof up !== "undefined") return;
        topPanel = tPanel;
        bottomPanel = bPanel;
        up = setInterval(this.goUp, 2);
        down = setInterval(this.goDown, 2);
    },

    goUp: function(x) {
        var h1 = document.getElementById(this.topPanel);
        if (h1.offsetHeight <= 0) {
            clearInterval(up);
            return;
        }
        h1.style.height = parseInt(h1.style.height) - 1 + "%";
    },

    goDown: function(x) {
        var h2 = document.getElementById(this.bottomPanel);
        if (h2.offsetHeight <= 0) {
            clearInterval(down);
            return;
        }
        h2.style.top = parseInt(h2.style.top) + 1 + "%";
        h2.style.height = parseInt(h2.style.height) - 1 + "%";
    }
};​

Give it a try on your own

Answer №3

Avoiding the use of jQuery may actually result in better performance compared to using it.

It's possible that you are considering changing your approach in order to have more precise control over animations, such as adjusting speed or easing effects. If this is your goal, you can experiment with the following solution:

http://jsfiddle.net/U3Lg9/13/

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

What's the reason behind beforeunload not triggering?

I've gone through numerous similar posts, but I'm struggling to figure out what's not quite right with this code snippet: $(window).on("beforeunload", function () { debugger handleBeforeUnload(); return false; }); function handl ...

bootstrap 5 with uneven spacing

I am working on some HTML/CSS code that utilizes Bootstrap 5.2 and the justify-content-between class in order to move two buttons to the edges of a container. <div class="container"> <div class="row"> <div cla ...

Choosing dropdown options without the <Select> tag in Selenium with Python

Looking to choose a date from the URL highlighted in yellow below: https://i.sstatic.net/UIBLJ.jpg Upon inspecting the HTML code, I couldn't locate any "select" tag as shown: https://i.sstatic.net/fhaaU.jpg Snippet of my code: chrome_options ...

Tips for styling a PHP file using CSS and styling more than one element:1. Begin by creating a

I am currently attempting to style a php file using a linked stylesheet, but I am encountering some difficulties. At the moment, I have the , it will affect either the font or the border.</p> Is there a way to apply multiple styles to elements on a ...

Steps for designing an animated background element

Hey there, I'm a newcomer here and I have a question that I couldn't find the right place to ask. I am currently working on creating my new portfolio website and I would like to add a moving cloud effect in the background. The idea is for the clo ...

Conceal and reveal a list on page load with jQuery

Is there a way to automatically display this listing collapsed without making changes to the HTML structure? I would like all folders in the tree to be collapsed. Instead of the initial display: https://i.stack.imgur.com/cDw4s.png I prefer it to look li ...

Is it possible for PHP to use the set cookie function to replace the cookie value set by JQuery cookie?

I'm facing an issue where I want a single cookie to be set and its value updated by PHP when a user logs in. However, currently it seems to just create a new separate cookie each time. Below is the code snippet where I am trying to set the cookie valu ...

Is there a way to prevent on-click errors in ReactJS that someone can share with me?

The onclick listener was expected to be a function, but instead received a value of string type. getListener@http://localhost:3000/static/js/bundle.js:18256:15 accumulateSinglePhaseListeners@http://localhost:3000/static/js/bundle.js:22846:39 <button on ...

Several directories containing references to PHP and CSS files

I'm encountering an issue with my PHP file named header.php. Here is the content of the file: <title><?php echo $title; ?></title> <link href="core/css/styles.css" rel="stylesheet" /> <link href="core/css/bootstrap.css" re ...

CSS is functioning properly on a local level, but fails to take effect once the "npm run build" command is executed in the React application's build

I am attempting to override the CSS of Muidrawer-paper. It works locally after running "npm start", but it does not take effect when building the package. .css-12i7wg6-MuiPaper-root-MuiDrawer-paper { position: absolute !important; top: 50px !import ...

Adjust the height of the embedded YouTube video iframe

I am trying to adjust the height of a YouTube video that is embedded in my webpage. After going through various threads, I found that adding an additional div outside the iframe element can solve this issue. However, the iframe element is already enclosed ...

The functionality of my input and button tags is compromised because of the background animation

I'm facing an issue with the username and password input fields, as well as the login button. These elements are placed on a rectangle within an SVG tag. The problem seems to be related to the animated background applied to one of the div elements in ...

Struggling with missing spaces when converting XML with HTML elements to CSV

I encountered an issue while trying to convert an XML file with HTML tags to CSV. For example, when I attempt to parse the following: "<strong>Nokia</strong> connecting people" from the XML description field using < strong > tags, in t ...

Dealing with unsuccessful ajax responses

My ajax request is not handling errors correctly. Earlier, the "r" was <br /> <b>Warning</b>: simplexml_load_string() [ <a href='function.simplexml-load-string'> function.simplexml-load-string and it did not ...

Eliminating date and time formatting in HTML emails on iPhones

I've designed an HTML email that is responsive and displays well on various platforms, but I'm encountering an issue specifically when viewing it on an iPhone. It appears that iOS is identifying a date and time in the content and applying a blue ...

How far apart are two surfaces when they are rotated along the Y-axis

Access Code: Click here to access the code I am trying to make sure that the red surface lies completely flat on top of the gray surface. However, there seems to be a gap (marked ??) between the two surfaces when I rotate 'modifier1'. How can I ...

The Ajax call functions perfectly on its own, but encounters issues when integrated with WordPress

I encountered an issue when transitioning this code from stand-alone to WordPress as it started to generate errors related to the ajax call. While initially struggling to fetch jQuery from an external source, I managed to download the file and make it run ...

The value of the checked input with the name `nameofinput` is currently not defined

I'm having trouble passing the input value to another page. $( this ).attr( "href", '?module=module_progress_report&Subject='+ $('input[name=subject]:checked').val()+ '&Centre_Selected_ID='+ encodeURIComponen ...

Creating a dynamic duplication feature for a form's text area

As a newcomer to the world of Javascript, I am embarking on creating a review-writing page. However, I have hit a roadblock in my progress. The main issue I am encountering is implementing a button that allows users to add a new section by duplicating an ...

sidebar that appears upon the initial page load

I'm currently working on implementing a sidebar navigation panel for my website using JavaScript, HTML, and CSS. However, I am facing an issue where the sidebar automatically opens when the page is first loaded, even before clicking on the icon to ope ...