javascript - jQuery - 在开始时找到 div 的 CSS 底部,而不是在悬停动画期间

标签 javascript jquery html css animation

我正在为每个具有“货架信息文本”类的 div 查找 CSS 值“底部”。这些 div 中的每一个都在 shelf-item 内。

底部值是使用另一个函数自动计算的。悬停时,我希望通过动画将底部更改为 0px,然后恢复为原始底部。每个 div 的底部都会不同。

我添加了一些代码来找到底部,但是如果您在动画中间再次悬停,它会在底部返回到原始状态之前找到底部,因此会破坏动画。

请告知我哪里出错了。谢谢。

var $itemBottom = null;
$('.shelf-item').hover(function() {
    $itemBottom = $(this).find('.shelf-info-text').css('bottom');
    $(this).find('.shelf-info-text').stop().animate({ 'bottom': '0px'}, 300);
}, function() {
    $(this).find('.shelf-info-text').stop().animate({ 'bottom': $itemBottom}, 300);
});

最佳答案

编辑: 重新阅读问题后,这里有一个解决方案,可以弄清问题并将其保存在数据属性中。

// we loop through all the shelf items and set a data attribute to keep track of it.
$('.shelf-item').each(function(){
    $item = $(this).find('.shelf-info-text');
    $itemBottom = $item.css('bottom');
    $item.data('bottom', $itemBottom);
});

$('.shelf-item').hover(function() {
    $itemBottom = $(this).find('.shelf-info-text').data('bottom');
    $(this).find('.shelf-info-text').stop().animate({ 'bottom': '0px'}, 300);
}, function() {
    $(this).find('.shelf-info-text').stop().animate({ 'bottom': $itemBottom}, 300);
});

关于javascript - jQuery - 在开始时找到 div 的 CSS 底部,而不是在悬停动画期间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19495926/

相关文章:

html - 如何向 css 类 .background-color-blue 添加更多特异性,以便它覆盖默认 div 的背景颜色

javascript - 如何将 div 旋转到特定坐标?

javascript - 通过匹配id映射2数组

javascript - 使用 webpack 获取 ractive 模块中的缩小版本

javascript - 逗号分隔语法中的变量含义/值

javascript - 删除跨度之间的空白

javascript - jQuery - 查找其中包含多个 HTML 标记属性的元素

jquery - 从多个表中选择第一行

html - 输入文件未在 IE 上打开文件上传框

javascript - 如何修复刽子手游戏中的开始功能