Javascript图像悬停效果

时间:2013-08-01 20:29:15

标签: javascript css

我想获得这种理想的悬停图像效果?

http://www.colingrist.com/

我想知道是否有人知道这些代码? 当您将图像悬停时,它会移动。 提前谢谢。

2 个答案:

答案 0 :(得分:4)

How do I view source of a webpage?

// Paralax Homepage Banner

    jQuery("#banner").mousemove(
        function(e){
            /* Work out mouse position */
            var offset = jQuery(this).offset();
            var xPos = e.pageX - offset.left;
            var yPos = e.pageY - offset.top;

            /* Get percentage positions */
            var mouseXPercent = Math.round(xPos / jQuery(this).width() * 100);
            var mouseYPercent = Math.round(yPos / jQuery(this).height() * 100);

            /* Position Each Layer */
            jQuery(this).children('img').each(
                function(){
                    var diffX = jQuery('#Parallax').width() - jQuery(this).width();
                    var diffY = jQuery('#Parallax').height() - jQuery(this).height();

                    var myX = diffX * (mouseXPercent / 1500); //) / 100) / 2;


                    var myY = diffY * (mouseYPercent / 1080);


                    var cssObj = {
                        'left': myX + 'px',
                        'top': myY + 'px'
                    }
                    //jQuery(this).css(cssObj);
                    jQuery(this).animate({left: myX, top: myY},{duration: 50, queue: false, easing: 'linear'});

                }
            );

        }
    );

http://www.colingrist.com/wp-content/themes/mill/scripts/functions.js

答案 1 :(得分:0)

看看这个:)你当然需要jquery库

http://jsfiddle.net/yFMeJ/

jQuery("#banner").mousemove(
        function(e){
            /* Work out mouse position */
            var offset = jQuery(this).offset();
            var xPos = e.pageX - offset.left;
            var yPos = e.pageY - offset.top;

            /* Get percentage positions */
            var mouseXPercent = Math.round(xPos / jQuery(this).width() * 100);
            var mouseYPercent = Math.round(yPos / jQuery(this).height() * 100);

            /* Position Each Layer */
            jQuery(this).children('img').each(
                function(){
                    var diffX = jQuery('#Parallax').width() - jQuery(this).width();
                    var diffY = jQuery('#Parallax').height() - jQuery(this).height();

                    var myX = diffX * (mouseXPercent / 1500); //) / 100) / 2;


                    var myY = diffY * (mouseYPercent / 1080);


                    var cssObj = {
                        'left': myX + 'px',
                        'top': myY + 'px'
                    }
                    //jQuery(this).css(cssObj);
                    jQuery(this).animate({left: myX, top: myY},{duration: 50, queue: false, easing: 'linear'});

                }
            );

        }
    );