マウスを乗せると背景画像が動く

Ads

Result

jQuery

distance = 100;
sensitivity = 17;
var distHalf = distance / 2;

var parW = $('#par').width(),
    parH = $('#par').height();

$('.layer').each(function() {
    var layer = $(this);

    pixPos = distance * (layer.index() + 1);
    pixPosHalf = distHalf * (layer.index() + 1);

    layer.width('+=' + pixPos);
    layer.height('+=' + pixPos);
    layer.animate({left: '-' + pixPosHalf }, 0); 
    layer.animate({top: '-' + pixPosHalf}, 0); 
    
    wDiff1 = $('#par').width();
    hDiff1 = $('#par').height();
    wDiff2 = layer.width();
    hDiff2 = layer.height();

    var wDiff = ((wDiff2 / wDiff1) - 1).toFixed(2);
    var hDiff = ((hDiff2 / hDiff1) - 1).toFixed(2);

    $('#par').mousemove(function(mousEv) {
        parOffset = $(this).offset();
        mouseX = (mousEv.pageX - parOffset.left);
        mouseY = (mousEv.pageY - parOffset.top);
    });

    var parWhalf = parW / 2,
        parHhalf = parH / 2;
    var mouseX = parWhalf,
        mouseY = parHhalf;
    var posX = parWhalf,
        posY = parHhalf;

    setInterval(function() {
        posX += (mouseX - posX) / sensitivity;
        posY += (mouseY - posY) / sensitivity;
        layer.css({
            left: '-' + Math.round(posX * wDiff) + 'px',
            top: '-' + Math.round(posY * hDiff) + 'px'
        });
    }, 30);

});

css

#par{
    position:relative;
    margin:150px auto;
    width:400px;
    height:200px;
     overflow: hidden;
}
.layer{
   position:absolute;
    width:400px;
    height:200px;
    z-index: -2;
    background:url(http://public-domain-photos.com/free-stock-photos-4-big/travel/los-angeles/los-angeles-skyline.jpg);
}
.layer:hover{width:800px;height:400px;}
.layer p{position:relative;}

html

<div id="par">       
    <div class="layer l1">     
        
    </div>                     
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>

via

div要素で作る簡単なパララックス効果の応用