简体   繁体   English

jQuery使用背景图像位置更改链接的悬停状态

[英]jQuery to change hover state of a link using background image position

I have an existing site, and like most of my cases I have to use a jQuery override to change some css. 我有一个现有站点,并且像大多数情况一样,我必须使用jQuery重写来更改某些CSS。 I can resize the element and give it a background image, but I can't get the hover state to work. 我可以调整元素的大小并为其提供背景图像,但是我无法使悬停状态起作用。

You can see my working JSfiddle here: http://jsfiddle.net/wdNF6/ 您可以在这里看到我正在工作的JSfiddle: http : //jsfiddle.net/wdNF6/

The big that seems to not work is 似乎不起作用的大是

$('.slideshow-screen .slide-item-current .play-button').hover( function(){
        $(this).css('background-position', 'something something');
}

I used the .hover() from examples I found throughout stackoverflow. 我从整个stackoverflow中找到的示例中使用了.hover() When you go to the example, the button should be blue with an image in it. 在转到示例时,按钮应为蓝色,其中带有图像。 Hovering over it should reposition the bg image. 将鼠标悬停在其上方应重新定位bg图像。 But when you go to the example, the default css pre-jQuery shows. 但是,当您转到该示例时,将显示默认的css jQuery pre之前的版本。 If you remove the second bit of jquery, the first part works. 如果删除jquery的第二位,则第一部分有效。

Any suggestions? 有什么建议么?

Missing the closing brace for the function 缺少该功能的右括号

$('.slideshow-screen .slide-item-current .play-button').hover( function(){
            $(this).css('background-position', 'bottom center');
    }​); <--- Missing this

Also this can be simply be written as 也可以简单地写成

$('.play-button').css({
    width: '90px',
    height: '90px',
    'background-color': '#0000ff',
    'background-position': 'top center',
    'background-image': 'url(http://www.lessardstephens.com/layout/images/slideshow_big.png)'
});

$('.play-button').hover(function() {
    $(this).css('background-position', 'bottom center');
});​

Check Fiddle 检查小提琴

Did you check? 你检查了吗? Your code had a typo. 您的代码有错字。

You missed an ending ); 您错过了结尾); at the end. 在末尾。

$(document).ready(function () {
    $('.slideshow-screen .slide-item-current .play-button').css({
        width: '90px',
        height: '90px',
            'background-color': '#0000ff',
            'background-image': 'url(http://www.lessardstephens.com/layout/images/slideshow_big.png)',
            'background-position': 'top center'
    });

    $('.slideshow-screen .slide-item-current .play-button').hover(function () {
        $(this).css('background-position', 'bottom center');
    },
    // Also you need to reset
    function () {
        $(this).css('background-position', 'top center');
    });
});​

Fixed: http://jsfiddle.net/Vc84h/ 固定: http//jsfiddle.net/Vc84h/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM