小编jrh*_*man的帖子

如何将重复的jQuery代码整合到一个函数中?

jQuery初学者在这里.

这是我正在做的事情.我有一个有10个热点的区域地图.将鼠标悬停在每个热点上,然后移动div的背景(id = dialpad)以显示其他数据(在精灵中).

我目前使用的代码,但我为每个热点ID都有一个单独的函数.

例如:

    $('#dial1')
    // On mouse over, move the background on hover
   .mouseover(function() {
     $('#dialpad').css('backgroundPosition', '0 -120px');
   })
   // On mouse out, move the background back
   .mouseout(function() {
     $('#dialpad').css('backgroundPosition', '0 0');
   })
     $('#dial2')
    // On mouse over, move the background on hover
   .mouseover(function() {
     $('#dialpad').css('backgroundPosition', '0 -240px');
   })
   // On mouse out, move the background back
   .mouseout(function() {
     $('#dialpad').css('backgroundPosition', '0 0');
   });
   ...
Run Code Online (Sandbox Code Playgroud)

我想要做的是将该代码合并到一个函数中,我只是从每个区域ID传递垂直偏移图.

有人可以帮忙吗?

css jquery

4
推荐指数
1
解决办法
326
查看次数

标签 统计

css ×1

jquery ×1