jQuery抓取高度和运行函数仅用于此项
jQuery grab height and run function only for this item
我有一个图像库,每个图像都有不同的大小,我想在悬停的图像上垂直居中放置一个图标。我一直在尝试用下面的代码来解决这个问题,我成功地获取了图像的所有高度,但我不知道如何将图像悬停的一个特定高度应用到等式中并运行函数。以下是我的代码:
$(document).ready(function() {
var wrapper = $('.img-wrap', this); // Grab the wrapper, same as image size
wrapper.prepend("<div class='cover'><img src='imgs/icons/search.png' alt='zoom into photo'></div>") // this is not so important
wrapper.each(function() {
$this = $(this);
var $height = $this.height(); // Get height of Wrapper or Image (same size)
var $marginHeight = ($height - 32) / 2; // Height of Wrapper - Icon Size (32px) and divided by two to only get one half
console.log($height) // Check height
console.log($marginHeight) // Check one half
// Main problem
wrapper.hover(
function() {
$('.cover img', this).css({
"marginTop": $marginHeight
});
});
// this runs fine
wrapper.mouseout(
function() {
$('.cover img', this).css({
"marginTop": 0
});
});
});
});
$(document).ready(function() {
$('.img-wrap')
.prepend("<div class='cover'><img src='imgs/icons/search.png' class="cover_img" alt='zoom into photo'></div>")
.on('mouseenter mouseleave', function(e) {
var h = e.type == 'mouseenter' ? ($(this).height() - 32) / 2 : 0;
$(this).find('.cover_img').css('margin-top', h);
});
});
FIDDLE
相关文章:
- 将函数的上下文应用于javascript变量
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 如何重写下面的函数,使其不会't用于循环
- Rails/Javascript链接到用于切换多个元素的函数
- 用于多个类事件Jquery的单个函数
- Javascript函数,用于将数组转换为“;2-元组”;
- 文本编辑后,append函数不适用于文本区域
- 文本表示法VS.构造函数,用于在JavaScript中创建对象
- 用于Dynamic Div'的Javascript数学函数;s风格
- javascript点击函数不;不适用于ID和Class
- Jquery函数,用于检查每个表单字段
- 用于交换两个变量的 JavaScript 函数
- JavaScript函数,用于计算两个日期之间的年、月和天数
- 使用jquery验证函数不适用于select元素
- js事件,用于函数内部的when语句变为true
- javascript复制一个用于函数的变量
- Jquery 函数不适用于函数参数
- Javascript原型仅用于函数
- PHP转义字符串中的JS(用于函数)
- Knockout克隆不适用于函数实例