2017-06-16 6 views
1

イメージとテキストがマウスの動きとは逆方向に移動する視差効果を作成しました。それは、視差ラッパーと呼ばれる要素の内部で起こっています。しかし、私が要素の外に移動すると、イメージとテキストが元の位置に戻るようにします。私は要素の外でマウスの位置を検出しようとしましたが、何らかの理由で正しく発射されませんでした。Javascriptでの視差の問題

codepenリンクがある - https://codepen.io/rohitgd/pen/gRLNad?editors=1010

HTML

<div class="parallax-wrapper"> 
    <div class="layer" data-mouse-parallax="0.1"> 
    <img src="https://tympanus.net/Development/MorphingBackgroundShapes/img/1.jpg"/> 
    </div> 
    <div class="layer" data-mouse-parallax="0.3">REVERT</div> 
</div> 

CSS

body { 
     background-color:#fff; 
     padding: 100px; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    } 


    .parallax-wrapper { 
     width: 500px; 
     height: 300px; 
     background-color:#0c0c0c; 
     position: relative; 
     overflow: hidden; 

     .layer { 
      width: 80%; 
      height: 80%; 
      position: absolute; 
     left: 30px; 
      text-align: center; 
      line-height: 300px; 
      font-size: 38px; 
      color:#FFF; 
      transition: all 200ms ease-out; 
     } 

    } 

img { 
    width: 200px; 
    height: 200px; 
    position: relative; 
    top: 50px; 
    right: 70px; 
} 

マウスが外にあるとき、私が欲しいのJavascript

$(".parallax-wrapper").mousemove(function(e) { 
    var x = e.pageX - $(this).offset().left - $(this).width()/2; 
    var y = e.pageY - $(this).offset().top - $(this).height()/2; 

    $("*[data-mouse-parallax]").each(function() { 
    var factor = parseFloat($(this).data("mouse-parallax")); 
    x = -x * factor; 
    y = -y * factor; 

    $(this).css({ transform: "translate3d(" + x + "px, " + y + "px, 0)" }); 
    }); 
}); 

$(document).mouseleave(function(e) { 
    var target = $(e.target); 

    if(!target.is("div.layer")) { 
     alert('out of the element'); 
     e.stopPropagation(); 


    } 
}); 

ですパララックスラッパーイメージとテキストは元の位置に戻ります。

答えて

0

です。 Here's a correct versionまたは下記のコードを参照してください。

あなたが内/外にいるとき、私は背景の色を変えると、それはアラートよりも優れています。あなたはラッパー(黒の背景)を離れるとき、それは正しく今度は反転します。

ここで、REDが設定されている場合は、トランスフォームを原点にリセットできます。

// Trying to replicate the effect here - https://tympanus.net/Development/MorphingBackgroundShapes/ 
 

 
$(".parallax-wrapper").mousemove(function(e) { 
 
    var x = e.pageX - $(this).offset().left - $(this).width()/2; 
 
    var y = e.pageY - $(this).offset().top - $(this).height()/2; 
 

 
    $(".parallax-wrapper").css("background-color", "#00ff00"); // <-- EXIT 
 
    // reset transform here 
 
    
 
    $("*[data-mouse-parallax]").each(function() { 
 
    var factor = parseFloat($(this).data("mouse-parallax")); 
 
    x = -x * factor; 
 
    y = -y * factor; 
 

 
    $(this).css({ transform: "translate3d(" + x + "px, " + y + "px, 0)" }); 
 
    }); 
 
}); 
 

 
// this is the selector I changed from "document" to ".parallax-wrapper" 
 
$(".parallax-wrapper").mouseleave(function(e) { 
 
    var target = $(e.target); 
 

 
    if(!target.is("div.layer")) { 
 
     $(".parallax-wrapper").css("background-color", "#ff0000"); // <-- ENTER 
 
     e.stopPropagation(); 
 

 

 
    } 
 
});
\t body { 
 
\t \t background-color:#fff; 
 
\t \t padding: 100px; 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
\t } 
 

 

 
\t .parallax-wrapper { 
 
\t \t width: 500px; 
 
\t \t height: 300px; 
 
\t \t background-color:#0c0c0c; 
 
\t \t position: relative; 
 
\t \t overflow: hidden; 
 
\t \t 
 
\t \t .layer { 
 
\t \t \t width: 80%; 
 
\t \t \t height: 80%; 
 
\t \t \t position: absolute; 
 
     left: 30px; 
 
\t \t \t text-align: center; 
 
\t \t \t line-height: 300px; 
 
\t \t \t font-size: 38px; 
 
\t \t \t color:#FFF; 
 
\t \t \t transition: all 200ms ease-out; 
 
\t \t } 
 
\t \t 
 
\t } 
 

 
img { 
 
    width: 200px; 
 
    height: 200px; 
 
    position: relative; 
 
    top: 50px; 
 
    right: 70px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="parallax-wrapper"> 
 
\t <div class="layer" data-mouse-parallax="0.1"> 
 
    <img src="https://tympanus.net/Development/MorphingBackgroundShapes/img/1.jpg"/> 
 
    </div> 
 
\t <div class="layer" data-mouse-parallax="0.3">REVERT</div> 
 
</div>

+0

ありがとう!これはまさに私が探していたものでした。背景色を変える代わりに、私は@Archerが書いたことをしました。今は完璧に動作します! –

+0

お手伝いします。あなたのコードには別のバグがありますが、それはCSSのためだと思います。ラッパーを上部に残すと、イベントは発生しません。おそらく位置付けの問題があり、CSSを使用してラッパーを配置する別の方法を使用する必要があります。それは別の問題の問題で、視差などはありません**待ち**またはそうではないかもしれません。私はこれがcodepenでしか起こらないことに気づいた。ここにそれはうまく働いているので – pid

+0

それは間違いなくcodepenの問題です、もしあなたのウェブサイトにもあれば、テストしてください! – pid

3

マウスを離しても変換がリセットされません。あなたは、アラートを持っているところ、マウスは.parallax-wrapper、あなたが以前にそれを持っていたようではないdocumentを離れるとmouseleaveイベントがトリガされていることを...

$(".parallax-wrapper").mouseleave(function(e) { 
    $("*[data-mouse-parallax]").each(function() { 
    $(this).css({ transform: "translate3d(0, 0, 0)" }); 
    }); 
}); 

注意を、これを追加する必要があります。

ここで修正codepenは、私は、セレクタが間違っていたと思う...

https://codepen.io/anon/pen/ZyBgYJ

+0

私は '.mouseout()マウスは、同様の子要素を離れたときにイベントハンドラを起動でしょう'このケースで – Chiller

+0

@Chillerを使用して、より良いと思います。この場合、 'mouseleave'は簡単でクリーンです(つまり、余分なイベントを処理して無視する必要はありません)。 – Archer

+0

ありがとうございました。あなたの答えと@pidの答えを組み合わせました。今はまったく同じように動作しています! –

0

$(".parallax-wrapper").mouseleave$(document).mouseleaveを交換してください。

$(".parallax-wrapper").mousemove(function(e) { 
 
    var x = e.pageX - $(this).offset().left - $(this).width()/2; 
 
    var y = e.pageY - $(this).offset().top - $(this).height()/2; 
 

 
    $("*[data-mouse-parallax]").each(function() { 
 
    var factor = parseFloat($(this).data("mouse-parallax")); 
 
    x = -x * factor; 
 
    y = -y * factor; 
 

 
    $(this).css({ transform: "translate3d(" + x + "px, " + y + "px, 0)" }); 
 
    }); 
 
}); 
 

 
$(".parallax-wrapper").mouseleave(function(e) { 
 
     alert('out of the element'); 
 
});
body { 
 
    background-color: #fff; 
 
    padding: 100px; 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
} 
 

 
.parallax-wrapper { 
 
    width: 500px; 
 
    height: 300px; 
 
    background-color: #0c0c0c; 
 
    position: relative; 
 
    overflow: hidden; 
 
} 
 
.parallax-wrapper .layer { 
 
    width: 80%; 
 
    height: 80%; 
 
    position: absolute; 
 
    left: 30px; 
 
    text-align: center; 
 
    line-height: 300px; 
 
    font-size: 38px; 
 
    color: #FFF; 
 
    transition: all 200ms ease-out; 
 
} 
 

 
img { 
 
    width: 200px; 
 
    height: 200px; 
 
    position: relative; 
 
    top: 50px; 
 
    right: 70px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="parallax-wrapper"> 
 
\t <div class="layer" data-mouse-parallax="0.1"> 
 
    <img src="https://tympanus.net/Development/MorphingBackgroundShapes/img/1.jpg"/> 
 
    </div> 
 
\t <div class="layer" data-mouse-parallax="0.3">REVERT</div> 
 
</div>

+0

答えをありがとう! –

関連する問題