Question

Does anyone knows how to transform this jQuery effect, instead of having fixed sized images, I need to set the size in percentage (width:100%; height:auto) so they can be responsive. Any ideas?

    <script type="text/javascript">
$(function (){
    $('img.fade').each(function (){
        var $$ = $(this);

        var target = $$.css('background-image').replace(/^url|[\(\)'"]/g, '');

        $$.wrap('<span style="position: relative;"></span>')
            .parent() // span elements
            .prepend('<img>')
            .find('img:first')
            .attr('src', target);

        $$.css({
            'position' : 'absolute',
            'left' : 0,
            'top' : this.offsetTop 
            });

        $$.hover(function () {
            $$.stop().animate({
                opacity: 0.2
            }, 250);
        }, function () {
            $$.stop().animate({
                opacity: 1
            }, 350);
        });
    });
});

</script>
Was it helpful?

Solution

I think you mean that the background image isn't scaling.

You could use the css command "background-size" to solve this problem..

Values that could help you are "cover" or "contain": http://www.w3schools.com/cssref/css3_pr_background-size.asp

var target = $$.css('background-image').replace(/^url|[\(\)'"]/g, '').css({ 'background-size': 'cover' });

If this is not working, you could read out the actual absolute width of your image an set it as background-size. But then you need an resize-event for when you're scaling the browser window..

I would recommend to use two pictures and show/hide the pictures instead of using a background-image for the hover.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top