javascript - 背景位置变量不起作用

标签 javascript jquery css animation

你好,我正在尝试使用 jQuery 中的 .animate() 和 .hover() 方法使我的背景上下动画。在我的 DOM 中,我有一个 id="#menu"的 div,其中包含一个 UL 列表,每个列表在不同的 x 位置但具有相同的 y 位置。我的困境是出于某种原因我的变量不起作用。它返回为错误“无法读取空值的属性'1'。这意味着它不存在?

这是我的代码:

$("#menu li").hover(
 function () {
 var xPos = $(this).css('background-position-x')  
 $(this).animate({backgroundPosition: "("+ xPos + "0px)"}, 500); 

         }
 , 
 function () {
   var xPos = $(this).css('background-position-x')  
 $(this).stop().animate({backgroundPosition: xPos + "35px"}, 500); 

             }
);

最佳答案

我发现问题出在变量上,当我检查 alert(typeof xPos) 时,它返回未定义。我只需要将此答案转换为整数值。

关于javascript - 背景位置变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9821314/

相关文章:

javascript - CKEditor 4 : Uncaught TypeError: Cannot read property 'langEntries' of null

javascript - JS Canvas 游戏碰撞检测有问题

javascript - 我需要帮助阅读这段代码以了解它在做什么,以便我可以将它转换为 javascript

jquery - 带有滚动捕捉的水平滚动页面?

javascript - 使用 .call(this) 继承原型(prototype)

javascript - 使用 jquery 显示数据库结果的 php 循环中的特定文本

javascript - 捕获按钮上所有单击的跨浏览器方式是什么?

jquery - 是什么让我的菜单水平?

javascript - Jimdo : Add placeholder to html using javascript/jquery

html - 试图将表格放在 Bootstrap 行中,但表格的宽度跳过了该行