私は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);">
どんな助けでもいただければ幸いです。