gpt4 book ai didi

jQuery 将 css 应用于偏移(裁剪)显示的图像

转载 作者:太空宇宙 更新时间:2023-11-04 12:16:29 25 4
gpt4 key购买 nike

我想应用 css 来偏移在元素的自定义数据属性中设置的 % 显示的图像。

HTML:

<div class="cover">
<img src="/path_to_some_image.jpg" data-offset_y="15">
<img src="/path_to_some_image.jpg" data-offset_y="18">
<img src="/path_to_some_image.jpg" data-offset_y="24">
<img src="/path_to_some_image.jpg" data-offset_y="7">
</div>

jQuery:

$(document).ready(function() {
$(.cover img).load(function (offset_y) {
var cover_w = 850;
var cover_h = 315;
var img_w = $(this).width ();
var img_h = $(this).height ();
var real_img_h = (cover_w * img_h / img_w) - cover_h;

$(this).css ({ top: parseInt (real_img_h * offset_y / 100 * -1) + "px" });
});
})

我需要一些指导。

编辑:

$(document).ready(function() {
$(".brand-cover img").load(function(offset_y) {
var cover_w = 350;
var cover_h = 130;
var img_w = $(this).width ();
var img_h = $(this).height ();
var real_img_h = (cover_w * img_h / img_w) - cover_h;

$(this).css ({ top: (real_img_h * $(this).data("offset_y") / 100 * -1) });
});
})

最佳答案

不确定这是不是你想要的,但这里是代码

$(document).ready(function () {
$(".cover").find("img").each(function () {
var cover_w = 850;
var cover_h = 315;
var img_w = $(this).width();
var img_h = $(this).height();
var real_img_h = (cover_w * img_h / img_w) - cover_h;
$(this).css({
// top: parseInt(real_img_h * $(this).data("offset_y") / 100 * -1) + "px"
// this equally does the job
top: real_img_h * $(this).data("offset_y") / 100 * -1
});
});
});

然后确保父容器.cover和图片有一个CSS 相对位置

.cover, .cover img {position: relative}

JSFIDDLE

* jsfiddle 已更新

关于jQuery 将 css 应用于偏移(裁剪)显示的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28665605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com