简体   繁体   English

使用JQuery冻结CSS3过渡

[英]CSS3 Transition freezing with JQuery

I am trying to create a jquery plugin that zoom the background image of a div when its hovered. 我正在尝试创建一个jQuery插件,以在div悬停时缩放div的背景图像。 However, i'm having a very strange glitch. 但是,我遇到了一个非常奇怪的故障。 It works fine and smooth, but when the div is clicked, the CSS3 transition starts to freeze. 它运行良好且流畅,但是当单击div时,CSS3过渡开始冻结。 I've made a codepen example. 我做了一个codepen的例子。

http://codepen.io/maxjf1/pen/rLKOPb http://codepen.io/maxjf1/pen/rLKOPb

EDIT: I tryed the first Answer but it didn't work. 编辑:我尝试了第一个答案,但是没有用。 I've changed de code and the codepen example for better demonstration. 我更改了code和codepen示例以进行更好的演示。

var smooth = 'all 200ms ease-out';
$("[up-background-zoom]").hover(function() {
  debug = true;
  $(this).css({
    'background-size': 'inherit',
    'cursor': 'zoom-in',
    'border': '1px solid rgba(0, 0, 0, 0.15)',
    'box-sizing': 'border-box',
    'transition': smooth
  });
}, function() {
  $(this).css({
    'background-size': '',
    'background-position': '',
    'cursor': '',
    'border': '',
    'box-sizing': '',
    'transition': ''
  });
});
$("[up-background-zoom]").mousemove(function(e) {
  var parentOffset = $(this).parent().offset();
  var parentOffset = $(this).offset();
  var relX = e.pageX - parentOffset.left;
  var relY = e.pageY - parentOffset.top;
  var divw = $(this).innerWidth();
  var divh = $(this).innerHeight();
  var posX = ((100 / divw) * relX).toFixed(3);
  var posY = ((100 / divh) * relY).toFixed(3);
  if (debug) $(this).html('<pre>' + posX + '%<br>' + posY + '%</pre>');
  $(this).css({
    'background-position': posX + '% ' + posY + '%',
  });
});

I don't know what is wrong, any help apreciated 我不知道出什么问题了,没有任何帮助

http://codepen.io/anon/pen/pbKgAz http://codepen.io/anon/pen/pbKgAz

Don't call mouse events inside mouse events, that'll mess things up. 不要在鼠标事件中调用鼠标事件,这会弄乱事情。 Use separate handlers instead. 改用单独的处理程序。

var debug = true,
  hovered = false,
  smooth = 'all 0.1s ease-out',
  el = $("[up-background-zoom]");

el.mousemove(function(e) {
  var $this = $(this),
    parentOffset = $this.offset(),
    relX = e.pageX - parentOffset.left,
    relY = e.pageY - parentOffset.top,
    divw = $this.innerWidth(),
    divh = $this.innerHeight(),
    posX = ((100 / divw) * relX).toFixed(1),
    posY = ((100 / divh) * relY).toFixed(1);

  if (debug) $this.html('<pre>' + posX + '%<br>' + posY + '%</pre>');

  $this.css({
    'background-position': posX + '% ' + posY + '%',
  });
})

el.on('click', function(e) {
  if (hovered) {
    console.log(this);
    e.preventDefault();
  }
});

el.hover(function() {
  $(this).css({
    'background-size': 'inherit',
    'cursor': 'zoom-in',
    'border': '1px solid rgba(0, 0, 0, 0.15)',
    'box-sizing': 'border-box',
    'transition': smooth
  });
  hovered = true;
}, function() {
  $(this).css({
    'background-size': '',
    'background-position': '',
    'cursor': '',
    'border': '',
    'box-sizing': '',
    'transition': ''
  });
  hovered = false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM