Tips for Smoothing Out Your jQuery LavaLamp Effect

I'm currently developing a jQuery function to implement the "lavalamp" effect on my navigation bar.

Everything seems to be working smoothly, except for one issue. If you hover over an item (e.g., Community Service) and then move your mouse away from the navigation bar, the #movingBox div resets to the Home item as expected. However, only the #movingBox .left div is visible while the other two disappear.

I need to fix this glitch before I can use it in a production environment.

HTML

<div id="navArea">
    <div id="navBar" class="noscript">
        <ul class="parent">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">After Care</a></li>
        <li><a href="#">Repair</a></li>
        <li><a href="#">Store</a>
            <ul class="sub">
                <li><a href="#">Cat 1</a></li>
                <li><a href="#">Cat 2</a></li>
                <li><a href="#">Cat 3</a></li>
                <li><a href="#">Cat 4</a></li>
            </ul></li>
        <li><a href="#">Events</a></li>
        <li><a href="#">Community Service</a></li>
        </ul>
    <div id="movingBox">
        <div class="left"></div>
        <div class="middle"></div>
        <div class="right"></div>
    </div>
    </div>
</div>

<div id="mainContent">
</div>

CSS

#navArea{
height: 48px;
width: 100%;
background-image: url(../img/navBG.png);
background-repeat: repeat-x;
font-size: 15px;
font-family: 'BPreplayBold', Helvetica, Arial, sans-serif;  
}
#navBar {
margin-right: auto;
margin-left: auto;
position: relative;
width: 636px;
}
#navBar ul{
padding: 0;
margin: 0;
z-index: 20;
position: absolute;
}
#navBar ul li{
float: left;
list-style: none;
}
#navBar ul li a{
padding-left: 15px;
padding-right: 15px;
height: 45px;
display: block;
text-decoration: none;
text-align:center;
line-height: 45px;
color:  #EBEBEB;
}
#navBar ul li ul li{
width: 222px;
float: none;    
}
#navBar ul li ul li a{
padding: 0px 15px;
display:block;
height:25px;
line-height:25px;
background: url('../img/navBG.png');
background-repeat: repeat-x;    
}   
#navBar ul li ul li a:hover{
background: #2abcf2;    
}

#navBar #movingBox {
height: 46px;
width: 70px;
left: 0;
top: 0;
z-index: 19;
position: absolute;
}
#navBar #movingBox .left {
background-image: url(../img/hBoxL.png);
float: left;
width: 7px;
height: 46px;
}
#navBar #movingBox .right {
float: left;
height: 46px;
width: 7px;
background-image: url(../img/hBoxR.png);
}
#navBar #movingBox .middle {
background-image: url(../img/hBoxM.png);
background-repeat: repeat-x;
height: 46px;
float: left;
width: 56px;
}

jQuery

<script type="text/javascript">
$(document).ready(function() {
$("#navBar").removeClass("noscript");
$("#navBar ul.sub").hide();
$("#navBar li").hover(function(){
    $(this).find('ul.sub').slideToggle(150);
});
$("#navBar li").mouseover(function(){
    hoverItemLeft = Math.round($(this).offset().left - $("#navBar").offset().left);
    hoverItemWidth = $(this).width();
    $("#movingBox").stop().animate({"left": hoverItemLeft, "width": hoverItemWidth}, 350, 'easeOutBack');
    $("#movingBox .middle").stop().animate({"left": hoverItemLeft, "width": hoverItemWidth-17}, 350, 'easeOutBack');
});
$("#navBar").mouseleave(function(){
    activeItemLeft = Math.round($("#navBar li.active").offset().left - $("#navBar").offset().left);
    activeItemWidth = $("#navBar li.active").width();
    $("#movingBox").stop().animate({"left": activeItemLeft, "width": activeItemWidth}, 350, 'easeOutBack');
    $("#movingBox .middle").stop().animate({"left": hoverItemLeft, "width": hoverItemWidth-17}, 350, 'easeOutBack');
});
});
</script>

Thank you for your assistance!

EDIT: Made a silly mistake on my end. In my last animate call, I unintentionally used the hoverItem variables. Feel free to use my code if you're looking for similar functionality, but remember to correct it.

Answer №1

Your JavaScript code seems to have a small error in the last few lines. Here...

$("#movingBox .middle").stop().animate({"left": hoverItemLeft, "width": hoverItemWidth-17}, 350, 'easeOutBack');

In the line above, please replace 'hoverItemWidth' with 'activeItemWidth';

To see the corrected version, you can view this fiddle - http://jsfiddle.net/joplomacedo/TxYe2/

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

An error was encountered: An identifier that was not expected was found within the AJAX call back function

I am experiencing an issue while attempting to query an API. An Uncaught SyntaxError: Unexpected identifier is being thrown on the success part of my JQuery Ajax function. $(document).ready(function(){ $('#submitYear').click(function(){ let year ...

What is causing the gap to appear between the image and the div element underneath?

I recently encountered an issue where I set up an image to scale to full width, but it was always cut off at the bottom when it extended too high. Here is my current setup: div, img, body { margin: 0; padding: 0; } #image { width: 100%; he ...

What is the best way to include a hyperlink in the same line?

I was curious about how to insert a hyperlink following some text: If you need more information on PCSE, make sure to check out the FAQ section on the PCSE website. (INSERT LINK HERE) <div class="panel-body"> <p>If you require any further inf ...

Extracting the inner content in the absence of an HTML element, only plain text

My website's platform has some unusual HTML that I need to work with. There is a section of code that looks like this: <div class="report-description-text"> <h5>Description</h5> Welcome to Ushahidi. Please replace this report with a ...

Having trouble with the functionality of JQuery drop feature?

As I delve into implementing drag and drop functionality with JQuery, I encounter a peculiar issue. I have set up 3 'draggable' divs and corresponding 3 'droppable' divs. The intended behavior is for each draggable element to be accepte ...

Issue with custom Javascript not executing in Internet Explorer versions 9 and 10

Initially, this script is found in the document's head section. <script> document.addEventListener("DOMContentLoaded", function () { var e, t = document.querySelectorAll("div.bounceInDown"); for (var n = 0, r = t.length; n & ...

Issues with Async Ajax functionality are being reported specifically in Safari browsers when a redirect

Encountering issues with an async Ajax call not functioning in Safari while working perfectly fine in other browsers. The problem arises when there is a redirect/link on the button or anchor that triggers the Ajax function. Here's a basic example: & ...

Enhance your online shopping experience with a personalized touch by incorporating an Ajax add to cart button on

Currently, I am working on customizing the add to cart button on a single product page. After implementing an ajax call solution from LoicTheAztec's post here, I have successfully added the feature. The code works perfectly fine, but now I am faced w ...

PHP, HTML, Email - Special characters in emails are being altered into other symbols

Here's the Situation... I am currently utilizing PHP to send out emails in a conventional manner... Snippet 0 mail('', $subject, $message, $headers); ... with the following content setup: Snippet 1 $boundary = uniqid('np&a ...

Leveraging node.js and express for incorporating custom stylesheets and scripts

I recently designed a webpage with the following elements at the beginning: <link href="./css/font-awesome.min.css" rel="stylesheet"> <link href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600" rel="stylesheet ...

What is the best way to title an uploaded chunk with HTML5?

Here is the script I am working with: function upload_by_chunks() { var chunk_size = 1048576; // 1MB function slice(start, end) { if (file.slice) { return file.slice(start, end); } else if (file.webkitSlice) { ...

Converting strings into the right values through parsing

I have a JSON response that contains multiple suggestions, but I only want to parse and extract the 4 "collation" results: jQuery191029421305245357143_1380819227858( { "responseHeader": { "status": 0, "QTime": 127 }, "command": ...

Maintain the default material-ui class styles while making customizations to override others

Currently, I am working on customizing the material-ui tooltip and specifically need to adjust the margin for the tooltipPlacementTop class: const useStyles = makeStyles(theme => ({ tooltipPlacementTop: { margin: '4px 0' ...

What is the method for inserting a vertical line between two div elements?

Can you show me how to add a vertical line between two div elements? .flex-container { display: -webkit-flex; margin: 0 auto; text-align: center; } .flex-container .column { width: 320px; text-align: center; } .vr { border-left: 2px dott ...

Tips on designing checkboxes with CSS

Can someone help me figure out how to style a checkbox using the code below? <input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" /> I've tried applying this style but it doesn't seem to work. The chec ...

Add a blur effect to a specific region of an SVG image

I have a complex, dynamic SVG image created using jQuery SVG. I want to create a "popup" area that appears on top of all SVG elements in the canvas. To achieve a modern translucent iOS7 style, I would like to add a blur filter to everything beneath the pop ...

Position the Bootstrap icon to float to the far right

My icon is always positioned to the right instead of beside the input field. Take a look at the image below: https://i.stack.imgur.com/Bb1eH.png The icon does not align next to the input field. This is my code snippet : <div class="form-horizontal"& ...

Using jQuery to Navigate through Different Routes in React Router

Hey there, I've managed to get my React-Router set up quite well but now I'm facing a new challenge. I need to implement some JavaScript functionality. For example: When the user enters their login details and clicks 'login', I want my ...

DataTables prioritizes sorting strings over numerical values

I am currently utilizing jquery.datatables to display numbers in columns within datatables. The numbers are formatted with spaces between thousands units (e.g. 123 456 789). However, this formatting causes the sorting to treat the numbers as strings rather ...

Can someone help clear up this confusion with CSS?

Why is image 6.png selected, when all the images are direct descendants of the div shape? Thank you for your assistance, it's greatly appreciated. As far as I know, it should select all the divs because they are all direct descendants of the div #shap ...