2

I'm using Jquery and im having trouble updating my CSS value on the fly during a hover event . Basically i want that little line at the bottom to scale to the scale in length to the size of the div.

Here is the problematic code in question:

$('.menu li').hover(function () {
   left = Math.round($(this).offset().left - $('.menu').offset().left);
   //find the current width of the div
   var width_flux = $('.menu #box .head').html($(this).find('img').width());
   //pass value to CSS 
   $('.menu #box .head').css('width', (parseInt(width_flux)) +'px');
   $('#box').stop(false, true).animate({left: left},{duration:500, easing: style}); 

   //if user click on the menu
});

and the corresponding css


.menu #box .head {
        background: url("http://whoisedward.com/img/bar.png");
        height:3px;
        width:1px;
        color:#eee;

        /* force text display in one line */
        white-space:nowrap;

        /* set the text position manually */
        padding-left:4px;
        padding-top:3px;
    }
2
  • 1
    First off, you might want to change hover to mouseenter as right now the javascript is throwing errors on hover off because the hover binds to both hover on and off. api.jquery.com/hover and api.jquery.com/mouseenter Commented Sep 28, 2012 at 22:41
  • var width_flux = $('.menu #box .head').find('img').width() should fix that. Right now it's returning an object, instead of an explicit integer. Commented Sep 28, 2012 at 22:43

3 Answers 3

3

There is problem in line

 var width_flux = $('.menu #box .head').html($(this).find('img').width());

width_flux is not with but jQuery element.

Modified code: Assuming you want use $(this).find('img').width() as width_flux

$('.menu li').hover(function () {
    left = Math.round($(this).offset().left - $('.menu').offset().left);
    //find the current width of the div
    var width_flux = $(this).find('img').width();
    //pass value to CSS 
    $('.menu #box .head').width(width_flux);
    $('#box').stop(false, true).animate({left: left},{duration:500, easing: style});    

    //if user click on the menu
})
Sign up to request clarification or add additional context in comments.

3 Comments

I think maybe he wants: $('.menu #box .head img').width();
I see, so how do I change it to fetch the div size or simply find the html width element on the page and store it in width_flux variable?
@Edward whose width you want use?
1

The line

var width_flux = $('.menu #box .head').html($(this).find('img').width());

is wrong. You add the width to the element, but the element (not the width) is assigned to width_flux. Obviously, <element>px is not a sensible CSS value.

Try:

width_flux = $(this).find('img').width();
$('.menu #box .head').html(width_flux);
$('.menu #box .head').css('width', width_flux + 'px');

Comments

1

You seem to be setting the value to you HTML and not getting it

var width_flux = $('.menu #box .head').html($(this).find('img').width());

So basically width_flux will be an element..

Instead try

var width_flux = $(this).find('img').width();

   OR 

var width_flux = $('.menu #box .head').html($(this).find('img').width());

   var width_flux = width_flux.html() ;
    // Then your code

Make sure you check for error conditions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.