简体   繁体   English

如何使用jQuery在图像上的CSS过滤器之间循环(在Webkit中)

[英]How to cycle through css filters on an image with jquery (in webkit)

I'm trying to impart a wild and psychedelic flair to a webpage by changing the css-filter hue-rotate property on an image when the user loads the page. 我正在尝试通过在用户加载页面时更改图像上的css-filter hue-rotate属性来赋予网页狂野和迷幻的色彩。 How do I get hue-rotate to change? 如何使色相旋转变化? I was trying to base my solution on this answered question: How to Cycle Through Background Colors on Hover with jQuery which was answered in this fiddle . 我试图根据以下已回答的问题提出解决方案: 如何使用jQuery在Hover上循环浏览背景色 ,此小提琴对此进行了回答。

    var counter = 0;
var colors = [
    "#eeeeee",
    "#00ff00",
    "#ff0000",
    "#000000"];

var $div = $('#coloredDiv');
var interval;
$('#coloredDiv').mouseenter(function () {
    interval = window.setInterval(changeColor, 1000);

}).mouseleave(function () {
    window.clearInterval(interval);
});

function changeColor() {
    var color = colors.shift();
    colors.push(color);
    $div.css({
        "background-color": color
    });

}

But instead of changing the background color on a div I am trying to change hue-rotate on an image within a div. 但是,我没有更改div上的背景颜色,而是尝试更改div内图像上的色相旋转。 And yes, I want it to change onload, not onmouseenter. 是的,我希望它更改加载而不是onmouseenter。 I am doing my testing in Chrome but Safari should also work since css filters have been implemented in webkit. 我正在Chrome中进行测试,但Safari也应该可以使用,因为Webkit中已实现CSS过滤器。

My attempt, which doesn't even try to incorporate the onload aspect yet: 我的尝试,甚至没有尝试结合onload方面:

var counter = 0;
var filters = [
    "hue-rotate(99deg)",
    "hue-rotate(22deg)",
    "hue-rotate(144deg)",
    "hue-rotate(270deg)";

var $div = $('#coloredDiv');
var interval;
$('#coloredDiv').mouseenter(function () {
    interval = window.setInterval(changeFilter, 1000);

}).mouseleave(function () {
    window.clearInterval(interval);
});

function changeFilter() 
    var filter = filters.shift();
    filters.push(filter);
    $div.css({

    "-webkit-filter": filter

    });

}

fiddle 小提琴

You had a fair few syntax errors. 您有一些语法错误。 I've fixed these here: http://jsfiddle.net/gvee/57E4K/4/ 我已经在这里修复了这些问题: http : //jsfiddle.net/gvee/57E4K/4/

var counter = 0;
var filters = ["hue-rotate(99deg)"
              , "hue-rotate(22deg)"
              , "hue-rotate(144deg)"
              , "hue-rotate(270deg)"];

var div = $('#coloredDiv');
var interval;

div.hover(function () {
    interval = window.setInterval(changeFilter, 1000);
}, function () {
    window.clearInterval(interval);
});

function changeFilter() {
    var filter = filters.shift();
    filters.push(filter);
    div.css({
        '-webkit-filter': filter
    });
}

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

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