1

I am making a mouse direction based gallery and i want the transition to be faster on the mouse out but i cant workout how to do it with this code JSfiddle demo. i have got the img and info panels set to different transition styles but i cant get the out styled also?

$(".gallery li").on("mouseenter mouseleave", function (e) {

      var w = $(this).width();
      var h = $(this).height();

      var x = (e.pageX - this.offsetLeft - (w / 2)) * (w > h ? (h / w) : 1);
      var y = (e.pageY - this.offsetTop - (h / 2)) * (h > w ? (w / h) : 1);

      var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4;

      /** check for direction **/
      switch (direction) {
      case 0:
          // direction top
          var slideFrom = {
              "top": "-100%",
              "right": "0"
          };
          var slideTo = {
              "top": 0
          };
          var imgSlide = "0, 100";

          break;
      case 1:
          // direction right
          var slideFrom = {
              "top": "0",
              "right": "-100%"
          };
          var slideTo = {
              "right": 0
          };
          var imgSlide = "-100, 0";

          break;
      case 2:
          // direction bottom
          var slideFrom = {
              "top": "100%",
              "right": "0"
          };
          var slideTo = {
              "top": 0
          };
          var imgSlide = "0, -100";

          break;
      case 3:
          // direction left
          var slideFrom = {
              "top": "0",
              "right": "100%"
          };
          var slideTo = {
              "right": 0
          };
          var imgSlide = "100, 0";

          break;
      }

      if (e.type === 'mouseenter') {
          var element = $(this);

          element.find(".info").removeClass("transform").css(slideFrom);
          element.find("img").addClass("transform-img").css("transform", "matrix(1, 0, 0, 1," + imgSlide + ")");

          setTimeout(function () {
              element.find(".info").addClass("transform").css(slideTo);
          }, 10);


      } else {
          var element = $(this);

          element.find(".info").addClass("transform").css(slideFrom);
          element.find("img").removeClass("transform-img").css("transform", "matrix(1, 0, 0, 1," + imgSlide + ")");

          setTimeout(function () {
              element.find("img").addClass("transform-img").css("transform", "matrix(1, 0, 0, 1,0,0)");
          }, 10);

      }
  });

1 Answer 1

2

In CSS, you could define different transition durations using :hover

.transform { /* mouseleave */
    transition: all 200ms cubic-bezier(0.19, 1, 0.22, 1) 0s;
}
li:hover .transform { /* mouseenter */
    transition-duration: 400ms;
}

With prefixes, of course.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that works great, i added it to both the image and info panel jsfiddle.net/unknown601/wL6rLsmq/10

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.