Change the position of a Div by clicking on a different Div using JQuery for custom movements

I have successfully managed to slide the div left/right based on the answers below, but I am encountering some issues with the top div. Here are the specific changes I am looking to make: 1. Make both brown lines thinner without affecting the animations. 2. Slide the div left/right when clicking on the vertical brown line and resize the width of the top div accordingly. 3. Slide the top div up/down when clicking on the horizontal brown line. I have made some progress with this but it's not perfect. Can someone please assist me further?

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="JQuery.js"></script>
<script src="JQuery-UI.js"></script>
<%-- <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>--%>
<link href="App_Themes/Design.css" rel="stylesheet" />
<link href="App_Themes/PageDesigns.css" rel="stylesheet" />
<title></title>
<script type="text/javascript">
    $(document).ready(function () {
        $("#showHideDiv").click(function () {
            if (!$('#divMenu').hasClass("off")) {
                $('#divMenu').animate({ 'margin-left': '-95%' }, 500);
                $('#divMenu').addClass("off");
                $('.div3').animate({ 'left': '-10%', 'width': '99.3%', 'margin-left': '-10.5%' }, 500);
                //$('.div3').animate({ 'left': '-10%', 'width': '99.3%', 'margin-left': '-10.5%' }, 500);

            }
            else {
                $('#divMenu').animate({ 'margin-left': '0px' }, 500);
                $('#divMenu').removeClass("off");
                $('.div3').animate({ 'left': '-0%', 'width': '89%', 'margin-left': '-0%' }, 500);
            }
        });
    });
</script>
</head>
<body>
<form id="form1" runat="server">
    <div style="float: left; width: 11%; height: 850px; padding: 0; margin: 0;">
        <div id="divMenu" class="">
            Sidebar- Menu
        </div>
        <div id="showHideDiv" style="float: left; height: 850px; width: 5%; background-color: brown;">
        </div>
    </div>
    <div class="div3" style="float: left; width: 89%; ">
        <div style="float: left; background-color: #93D209; width: 100%; height: 50px;display:block">
            hello
        </div>
        <div style="height: 0.5%; width: 100%; background-color: brown;display:block">
            hi
        </div>
    </div>
</form>
</body>
</html>

Answer №1

Check out the Updated Demo

Initially, I've set the CSS for .div3 as follows:

.div3{
    position: absolute;
    width: 89%;
    margin-left: 11%;
}

The margin-left is dependent on the sidebar's total width and positioned absolutely to prevent it from moving down. You can also use 'fixed' positioning if that suits your needs.

When the sidebar switch is clicked, .div3 animates its margin-left to 0% and expands its width to cover 99%-100% of the screen.

 $('.div3').animate({ 'margin-left': '0%', 'width': '99.3%'}, 500);

For toggling back the sidebar, .div3 animates back to its original margin-left of 11% and width of 89%.

 $('.div3').animate({ 'margin-left': '11%', 'width': '89%'}, 500);

Regarding the top switch, I incorporated this jQuery code:

 $("#switch2").click(function () {
        if (!$('#topDiv').hasClass("off")) {
            $('#topDiv').animate({ 'margin-top': '-95%' }, 500);
            $('#topDiv').addClass("off");

        }
        else {
            $('#topDiv').animate({ 'margin-top': '0px' },500);
            $('#topDiv').removeClass("off");
        }
    });

I assigned id attributes (#topDiv and #switch2) to relevant HTML tags like:

<div class="div3">
        <div id="topDiv">
            hello
        </div>
        <div id="switch2">
            hi
        </div>
</div>    

Overall jQuery script:

$("#showHideDiv").click(function () {
            if (!$('#divMenu').hasClass("off")) {
                $('#divMenu').animate({ 'margin-left': '-95%' }, 500);
                $('#divMenu').addClass("off");
                $('.div3').animate({ 'margin-left': '0%', 'width': '99.3%'}, 500);

            }
            else {
                $('#divMenu').animate({ 'margin-left': '0px' }, 500);
                $('#divMenu').removeClass("off");
                $('.div3').animate({ 'margin-left': '11%', 'width': '89%'}, 500);
            }
        });
     $("#switch2").click(function () {
            if (!$('#topDiv').hasClass("off")) {
                $('#topDiv').animate({ 'margin-top': '-95%' }, 500);
                $('#topDiv').addClass("off");

            }
            else {
                $('#topDiv').animate({ 'margin-top': '0px' },500);
                $('#topDiv').removeClass("off");
            }
        });  

Try the Working Sample

Answer №2

 <div class="container">

<div class="menu-btn">
<a href="javascript:void(0);">
    <h6>click me</h6> 
</a>

.container { width:150px; height:500px; background:#000; margin-left:-150px;

}

.menu-btn { width:50px; height:20px; background:#d2d2d2; position: fixed;

z-index: 999;top: 20px;left: 0;margin-left: 25px;}

var flagswap=0;
$(document).ready(function () {
  $('.menu-btn a').click(function()
{
    if(flagswap==0)
    {
        $('.container').animate({marginLeft:0},500);
        $('.menu-btn').animate({marginLeft:'170px'},500);
        flagswap=1;
    }
    else
    {
        $('.menu-btn').animate({marginLeft:'25px'},500);
        $('.container').animate({marginLeft:'-150px'},500);
        flagswap=0;
    }
});  
});

http://jsfiddle.net/uBhR6/

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

The TypeScript factory design pattern is throwing an error stating that the property does not

While working with TypeScript, I encountered an issue when trying to implement the factory pattern. Specifically, I am unable to access child functions that do not exist in the super class without encountering a compiler error. Here is the structure of my ...

Tips for increasing the size of a textarea

I'm attempting to extend a textarea by adjusting the margin-top property, but it doesn't seem to be working as intended. Here is the code snippet in question: #sqlcontainerLoggedInPage2 { margin-top: 60px; } <div class="container-fluid" i ...

Leveraging basscss through NPM/Webpack installation

As a newcomer to the webpack/react app environment, I am attempting to incorporate the Basscss CSS framework into my project. After successfully running 'npm i basscss' and adding require('basscss/css/basscss.css'); to my app entry poin ...

CSS files not loading on live-server

My HTML file is linked to a CSS file, but the CSS doesn't seem to load when I use live-server. Interestingly, the CSS works perfectly fine when I directly open the HTML file in a browser. The CSS file is located outside the directory where my HTML fi ...

Ways to correctly import various CSS files for individual routes in a Vuejs application

Currently, I am in the process of developing a project using Vue.js and Laravel. This project will have distinct layouts for the admin/user panel and landing page, requiring different CSS files to be loaded for each route. At the moment, I am utilizing va ...

Utilizing MS SQL, AJAX, PHP, and JSON to fetch dynamic page content based on dropdown selection

I've hit a roadblock in my project. I'm currently working on an event registration system where a user selects an event from a dropdown menu populated by values and IDs retrieved from a SQL stored procedure (index.php). Upon selecting an event, I ...

Adding up the values of an array based on their position in Javascript

I came across a JavaScript array that looks like this: var array1 = [ [1, 2, 3], [7, 9, 2], [6, 8, 1] ] What I'm aiming for is to get the following output: var array2 = [ 14, 19, 6 ] array1[0] = 1 + 7 + 6 array1[1] = 2 + 9 + 8 array1[2] = 3 + 2 + ...

What is the process for displaying a hidden div using a button?

I'm running into a problem with my project. Here is the code I've been using to hide my div element : @keyframes hideAnimation { to { visibility: hidden; } } To trigger the animation and hide the div after 6 seconds, I have implemented ...

Establish individual states for every dynamically created component using the same handler function

I have a component in React built with Material UI, where the child component (Paper) is dynamically generated depending on the number of items in an array. The challenge I'm facing is changing the elevation property of the Paper component when it&ap ...

Ordering ng-repeat in AngularJS using a separate arrayDiscover how to easily order your

Imagine I have an array containing keys in a specific order orderedItems=["apple","banana","orange]; and there is a JSON object that I want to display using ng-repeat but following the sequence specified in the array: {"fruits": { "apple":{ ...

Retrieve the 90 days leading up to the current date using JavaScript

I've been searching for a way to create an array of the 90 days before today, but I haven't found a solution on StackOverflow or Google. const now = new Date(); const daysBefore = now.setDate(priorDate.getDate() - 90); The result I'm looki ...

Display information from a specific identifier in a modal popup window after clicking a button using PHP and MySQL

When a button is clicked on my website, a popup window appears using modal functionality. The issue I am facing is not being able to retrieve the correct data based on the id of the button. Below is the HTML table code snippet: <tbody> <?php $cou ...

Encountering an issue with receiving "undefined" values while utilizing WordPress post metadata in AngularJS

Utilizing the Wordpress REST API v2 to fetch data from my functional Wordpress website to an AngularJS application. Everything is functioning properly, however when I attempt to access post meta such as "_ait-item_item-data", it returns an error stating "u ...

Transferring information from a Jade file to a Node.js server

I'm currently working on creating a data object within my Jade view page that will be used in my server-side JS. The data object involves dynamic HTML generation that inserts input boxes based on user input. function addDetail() { var det ...

What is the best way to adjust my content to be mobile-friendly with the Material UI drawer?

I am facing an issue with adjusting the content in Form.jsx based on the movement of the drawer. When I expand the Drawer by clicking on the menu icon, the content inside the body does not move along with it. How can I make sure that the content moves acco ...

Modify the class of a div as you scroll through the page between anchor points

I have been working on implementing a feature for my website where when I scroll to an anchor, the corresponding menu item adds or removes a specific class. However, I am facing an issue with making the first menu item remove the class when scrolling to t ...

Retrieving cached data using $http in AngularJS

When making a request to an API using $http in AngularJS, I am receiving cached results. Below is the AngularJS code snippet: $scope.validate = function(){ var encodedUserNameAndPassword = Base64.encode($scope.username + ':' + $scope.passwo ...

Incorporating image URI into HTML for validation purposes

My website validation is encountering issues, mainly due to data/images base64 URIs. I ran a diagnosis on Error: Malformed byte sequence: e9. At line 2, column 259 (etc.) The analysis from https://validator.w3.org/ simply states that it cannot anal ...

Integrating webpack with kafka-node for seamless communication between front

I am in the process of embedding a JavaScript code that I wrote into an HTML file. The script requires kafka-node to function properly, similar to the example provided on this link. To achieve this, I am using webpack to bundle everything together. I am fo ...

Creating a grid layout by designing boxes using CSS and HTML

I'm in the process of creating a homepage and I'd like it to resemble this design: (not the best quality, but the software I used was subpar (I miss mspaint)) Originally, I used buttons which made linking and customization easy, but I encounter ...