JavaScript Div Splitter with Event Handling

I am developing a basic HTML DIV splitter and have set the cursor as col-resize for it. I want the cursor to remain as col-resize while dragging the splitter, but if the mouse moves beyond the resize boundary, the cursor should revert back to its default shape. The HTML:

<div id="container">
    <div id="left"></div>
    <div id="splitter"></div>
    <div id="right">
</div>

The CSS:

#container {
    position: relative;
}

#left {
    position: absolute;
    left: 2px;
    top: 2px;
    width: 200px;
    height: 200px;
    outline: 2px solid red;
}

#right {
    position: absolute;
    left: 202px;
    top: 2px;
    width: 300px;
    height: 200px;
    outline: 2px solid blue;
}

#splitter {
    position: absolute;
    left:202px;
    top: 2px;
    height: 200px;
    z-index: 2;
    cursor: col-resize;
    width: 8px;
}

#splitter.active {
    background-color: gray;
}

Another issue I am facing is that I want the background color to change to gray when the mouse is down and dragging, but return to no color when the mouse is released or done dragging. However, if the mouse is outside of the splitter, the mouseup event does not trigger.

$("#splitter").on("mousedown mouseup", function() {
    $(this).toggleClass("active");});​

You can access the jsFiddle link here: http://jsfiddle.net/Shuping/MhYuK/ Do you have any suggestions?

Answer №1

The alternate condition has not been specified.

How about trying this instead?

$("#splitter").on("mousedown", function() {
    $(this).addClass("active");
});

$("#splitter").on("mouseup mouseleave", function() {
    $(this).removeClass("active");
});​

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

Implement a modal window function that appears once the form has been submitted

My second question for today requires your assistance once again. I have developed a small program that includes a text field for users to input their phone numbers. Upon entering the phone number, the program executes certain instructions to calculate a ...

Show the cost on the HTML page according to the chosen currency option

I am currently dealing with two primary currencies in my business. The product page is created using HTML. There are 4 products on a single page, and I wish to display two prices for each product based on the selected currency (USD or EUR) without having t ...

Select the small image to reveal the larger overlay image

I have a grid of images and I want each image to expand into fullscreen when clicked. However, the JavaScript I am using is causing only the last image in the grid to overlay. How can I resolve this issue? Here is the code I am currently using: http://js ...

What are some ways to showcase information using AJAX?

Hey there! I'm currently facing an issue with AJAX. I need help in extracting data and displaying it as a table in HTML format. The data I have is just dummy, but in reality, it consists of multiple objects, including nested ones. Can someone please a ...

What is the best way to maintain a font size of 150% even after refreshing the page or reopening it?

After successfully implementing two buttons that allow users to adjust the font size on my webpage, I noticed that upon refreshing or reopening the page, the font size resets to its default setting. How can I ensure that the font size remains at 150% even ...

Eliminating an undesired gap that exists between two ul li elements

Seeking assistance urgently. Any tips on eliminating the annoying small space that separates my 2 UL LI lists? I have a hunch it might be related to the <dd> tag, but pinpointing the exact issue is proving tricky. Check out this visual representati ...

Display a button only when hovering over it

Seeking assistance for a simple task that is eluding me at the moment. I am currently using scss and trying to make a button only appear when hovered over. The button is hidden in the code snippet below, nested within a block alongside some svgs. Any hel ...

Swift guide to adjusting font size within NSAttributedString, all while preserving HTML styling

My UITextView is displaying a formatted HTML string, but when I try to adjust the font size using Increase or Decrease buttons, the HTML style and font are lost. Is there a way to change the font size without losing the HTML formatting? import UIKit exte ...

Tips for iterating through JSON Data:

Struggling with looping through JSON data retrieved from an ashx page to add values to li's in a ul. How can I effectively loop through and extract the values from the following JSON data? The format of the returned data looks like this: {"ImageCol ...

What's the best way to reduce the dimensions of this container in order to align them next to one another?

https://i.sstatic.net/VHahe.jpg As a novice student in the world of coding with html and css, I have embarked on a project to create a website. However, I've encountered a minor issue... I'm looking to adjust the width of these boxes so they al ...

Parent whose height is less than that of the child element

I'm working on creating a side menu similar to the one on this website. However, I'm facing an issue with the height of the #parent list element. I want it to match the exact same height as its content (the #child span - check out this jsfiddle). ...

"Why is it that my data is spread across multiple tables instead of being organized into one

When creating a table and entering data into it, I am facing an issue where the data is not properly arranged within the table. Instead, it appears outside the table border and in a disorganized manner. I expect the data in the table to be neatly arranged ...

Guide to modifying the text color within a textarea

I want to create a dynamic text area where the color of certain words changes as I type. For instance, if I input the following text: He went to the market to buy an apple The word "market" should turn green The word "apple" should become red This is m ...

Using Jquery to show element when <select> is updated

I've been struggling to make this work due to my limited jquery/js skills. My goal is to have a different message displayed for each option selected from the dropdown menu for further information. <label class="checklabel">Is it possible to re ...

Utilizing Bootstrap 4 dropdowns for seamless section navigation

I have come across various examples in Bootstrap 3 where a dropdown is used to tab between different sections. However, I prefer to utilize Bootstrap 4 in my application. In Bootstrap's documentation, I noticed that tab navigation links can be impleme ...

The Film Repository: Retrieve Movie Synopsis JSON

Exploring the process of connecting to The Movie Database API with JSON and retrieving specific movie information. Currently, I can successfully retrieve "poster_path", "original_title", and "release_date" but am struggling to access the movie overview. De ...

Challenge with implementing custom http headers in jQuery's $.ajax请求

I need help with querying a REST webservice that requires custom http headers for authentication. When I make a POST request without the headers, I get the expected error. However, when I include the headers, I receive a 404 error instead of the desired r ...

prevent the page from scrolling to the top when clicking on an empty <a> tag

Is it possible to prevent the page from scrolling back to the top when a user clicks on an empty link tag using only HTML and without JavaScript? For example, if there is an empty link at the bottom of the page and a user clicks on it, the page jumps bac ...

Supply a JSON parameter as a variable into the .load() function

When a button is clicked, I am attempting to load a page using the .load() method with a POST request. The URL parameters are generated and displayed as JSON formatted in the button attribute btn-url. Problem: The parameter is not being passed to the .loa ...

Struggling to perfectly align a Wufoo form within an iframe using CSS

I have integrated a web form from Wufoo into my webpage using an iframe. Custom CSS can be used to style the form and I am trying to center it within the iframe. Below is the HTML code of the form displayed in the iframe: <div id="container" class="lt ...