0

Here is my code:

jQuery:

$(document).ready(function () {
        $("#news").hover(function () {
            $('#news_img').animate({
                height: 'toggle'
            }, 290, function () {
            });
        });

        $("#news1").hover(function () {
            $('#news_img1').animate({
                height: 'toggle'
            }, 290, function () {
            });
        });

        $("#news3").hover(function () {
            $('#news_img3').animate({
                height: 'toggle'
            }, 290, function () {
            });
        });

        $("#news4").hover(function () {
            $('#news_img4').animate({
                height: 'toggle'
            }, 290, function () {
            });
        });
    });

JSFIDDLE here: http://jsfiddle.net/huydq91/N89Kw/

I would like to reduce my code and make it easier to manage in the future whenever I would love to add more <tr> or <td> tags without editing too much in the jQuery and CSS.

3
  • place your code in the question. Commented Apr 8, 2014 at 3:24
  • Just jQuery code would be enough! Commented Apr 8, 2014 at 3:34
  • OK. This has fixed already! Commented Apr 8, 2014 at 3:41

3 Answers 3

4

You can target the hover elements by its class news and find the target element by appending the last digits in the hovered element's id to news_img like

$(document).ready(function () {
    $(".news").hover(function () {
        $('#news_img' + this.id.replace('news', '')).stop(true).animate({
            height: 'toggle'
        }, 290, function () {});
    });
});

Demo: Fiddle


You can remove the css part of the hover by adding some data-* attributes to the image like

<img src="http://i853.photobucket.com/albums/ab100/vimeo1903/Showroom1_zps923b43dc.jpg" border="0" alt="Showroom1" data-hover="http://i853.photobucket.com/albums/ab100/vimeo1903/Showroom1_1_zpse41d0851.jpg" />

then

$(document).ready(function () {
    //since the news elements has a common class, use it to target all the news elements instead of using individual ids
    $(".news").hover(function (e) {
        //you can find the `news_img` within the current news item using .find() instead of using its class to target it
        $(this).find('.news_img').stop(true).animate({
            height: 'toggle'
        }, 290);
        //find the image element within the current news
        var $img = $(this).find('.imgswap img');
        //if the current event is mouseenter then show the hover image else the src image
        //the hover handler registers 2 handler one for mouseenter another for mouseleave
        $img.attr('src', $img.data(e.type == 'mouseenter' ? 'hover' : 'src'));
    });
    //when we leaves the news elements we need to put back the original src, so store it using data api
    $('.news .imgswap img').each(function () {
        $(this).data('src', this.src);
    })
});
Sign up to request clarification or add additional context in comments.

15 Comments

That works well. However, I am wondering about your code and Lloyd Banks's code, which one will help me to optimize the server?
@PMay1903 also see jsfiddle.net/arunpjohny/fz5eL/2 - it stream lines the hover image to be added to the image element itself instead of creating a new css style
@Arun P Johny: Your last updated code is petty nice and I don't need to add anything to CSS as my Title. Those are what I am looking for. I appreciate all your help and Lloyd Banks.
@Arun P Johny: Could you please add comments to your code in order to make it clear?
1. $img.attr('src', $img.data(e.type == 'mouseenter' ? 'hover' : 'src')); 2. $(this).data('src', this.src);--> These make me so confused...
|
2

Combine your jQuery calls into one function family. Instead of 4 separate .hover() calls, use class names and do the following:

$(document).ready(function () {
    $(".news").hover(function(){
        $(this).find(".news_img").animate({
            height: "toggle"
        }, 290, function(){
        });
    });
});

On your CSS, you're pretty compact already and there's really not much more you can do to reduce the amount of code you have.

Updated fiddle

1 Comment

Thank you for your code. Please let me know that your code with find will help my server running faster than the Arun P Johny's replace?
2

Use attribute selector in jquery.

 $(document).ready(function () {
        $("[id^=news]").hover(function () {
                $('#news_img').stop().animate({
                    height: 'toggle'
                }, 290, function () {
                });
    });
 });

Fiddle

6 Comments

which toggle Please look at the fiddle and check let me know
This doesn't operate on a different news_img for each one like the OP's code.
All toggle for each image. Please take look on my JSFiddle and mouse over to each image, you will see the toggle goes up.
sorry i missed the braces
It's great. You are giving me one more choice for my code and it would be useful so much!
|

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.