0

我正在使用 clip:rect 将图像剪辑成缩略图,并且我想随机放置剪辑矩形在翻转时的位置。我的代码:

    $('.itembox').mouseover(function() {
    var img = $(this).find('img');
    var width = img[0].width;
    var height = img[0].height;
    var clipwidth = Math.floor( $(this).width() );
    var clipheight = Math.floor( $(this).height() );

    var widthrange = width - clipwidth;
    var heightrange = height - clipheight;

    var x = Math.floor(Math.random() * widthrange);
    var y = Math.floor(Math.random() * heightrange);
    var x2 = x + clipwidth;
    var y2 = y + clipheight;

    img.css('clip', 'rect('+x+'px '+x2+'px '+y+'px '+y2+'px)');
    $(this).children('.itemboxbg').show();
    $(this).css({'color' : 'white'});

});

它适用于预设的 css:

.itemboxbg {
    border: none;
    position:absolute;
    width:193px;
    height:273px;
    z-index:0;
}
.itemboxbg img {
    border: none; 
    position: absolute; 
    clip: rect(0px 193px 273px 0px); 
}

但是当翻转事件触发并更改 css 时,图像就会消失。CSS很好,例如:

<img src="./img/exampleimage.png" style="clip: rect(304px 498px 72px 344px);">

任何帮助,将不胜感激。

4

1 回答 1

0

愚蠢的,它应该是:

img.css('clip', 'rect('+y+'px '+x2+'px '+y2+'px '+x+'px)');

由于 clip: rect 已定义

rect(<top> <right> <bottom> <left>);

其中<top><bottom>指定从框的上边框边缘的偏移量,以及<right><left>指定从框的左边框边缘的偏移量。

虽然还不是解决方案,因为图像没有出现在框中,也许我应该使用背景位置

编辑:确定更改绝对左侧和顶部位置将图像移回框中:

img.css('left', -x+'px');
img.css('top', -y+'px');
于 2012-12-19T02:00:21.643 回答