JQuery / JS / ASP - 让元素 (div) 像 StackOverflow 一样淡入淡出

发布于 2024-12-08 20:58:36 字数 1419 浏览 1 评论 0原文

在 StackOverflow 网站上,您会注意到左上角的“通知”指示器。当有人回复您的问题/答案时,您单击通知,它会将您定向到该特定回复,然后以橙色背景显示,然后慢慢淡入白色,让您知道您正在查看哪个回复。我想知道如何实现这种淡入淡出方法。

我想要闪烁的元素是 div。下面是我的 DIVS 的排列方式,因为它们是由 ASP 动态生成的:

...
<div id="1046" class="photoBlob">........</div>
<div id="1047" class="photoBlob">........</div>
<div id="1048" class="photoBlob">........</div>
...

如您所见,它已经包含样式 (class="photoBlob"),背景是透明的,直到鼠标悬停时,背景变为灰色。

我需要刷新的特定 DIV 来自查询字符串 (photos.asp?photoID=1047)。我所说的“闪光”是将 DIV 的背景更改为彩色 (#19426E),然后在 2 秒后将该颜色淡化回透明。

如果有一个要刷新的 DIV 并且我知道要刷新的 DIV ID,我可能可以解决这个问题,但是来自查询字符串,我不知道我在做什么。如果有任何建议、示例或片段帮助我开始这方面的工作,我将不胜感激。我想我找到了用于闪烁元素的 JQuery 插件,但即便如此,我如何使用我的查询字符串“photoID”来提供该插件,我的 JS 显然是垃圾!

非常感谢

我的回答 - 感谢(150PoundsOfDonamite)

我实际上犯了一个错误,我的div的id不是来自查询字符串,它来自URL的锚点/哈希部分。因此,感谢已接受的答案(如下),我成功地完成了这项工作 - 看起来很不错!

我添加了 JQuery 插件: http://www.bitstorm.org/jquery/color-animation/

然后我添加了这个 JQuery/Javascript:

$(document).ready(function() {
    var flashDiv = window.location.hash;

    if(flashDiv!==false) {
        setTimeout(function() {
            $(flashDiv).animate({backgroundColor: '#19426E'}, 2000);
            $(flashDiv).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
        }, 1000);
    }
});

On StackOverflow website, you will notice your 'Notifications' indicator at the top left. When somebody replies to your question/answer, you click the notification and it directs you to that particular reply, then displays with an orange background and slowly fades to white, to let you know which reply you're looking at. I would like to know how I can achieve this fade method.

The element I would like to flash is a div. Below is how my DIVS are arranged as they are dynamically produced by ASP:

...
<div id="1046" class="photoBlob">........</div>
<div id="1047" class="photoBlob">........</div>
<div id="1048" class="photoBlob">........</div>
...

As you can see, it already contains styles (class="photoBlob"), the background is transparent until mouse-over, when it changes to grey.

The particular DIV I need to flash comes from a query string (photos.asp?photoID=1047). What I mean by flash, is to change the background of the DIV to color (#19426E) and then fade that color back to transparent after 2 seconds.

I could probably work it out if there was one DIV to flash and that I knew the DIV ID to flash, but coming from a query string, I have no idea what I am doing. I would be grateful for any suggestions, examples or snippets to get me started with this. I think I found JQuery plugins for flashing elements but even then, how do I feed that plugin with my query string 'photoID', my JS is rubbish obviously!

Many thanks

MY ANSWER - Thanks to (150PoundsOfDonamite)

I actually made a mistake, my div's id was NOT coming from a query string, it was coming from the anchor/hash part of the URL. So thanks to the accepted answer (below), I managed to get this working - and looks the biz!

I added the JQuery plugin: http://www.bitstorm.org/jquery/color-animation/

I then added this JQuery/Javascript:

$(document).ready(function() {
    var flashDiv = window.location.hash;

    if(flashDiv!==false) {
        setTimeout(function() {
            $(flashDiv).animate({backgroundColor: '#19426E'}, 2000);
            $(flashDiv).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
        }, 1000);
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2024-12-15 20:58:36

这是一个颜色渐变器,知识共享许可证。
http://www.scriptiny.com/2008/05/javascript- color-fading-script/

我改进了该代码以支持透明度。

工作演示:
http://jsbin.com/eceriv

不需要 jquery、mootools 或任何其他框架。

代码中有趣的部分是这样的:

// incrementally close the gap between the two colors
function animateColor(element,property) {
    var i, rgb, fadeState = element.fadeState;
    if (fadeState.step <= fadeState.nSteps) {
        for (i=0; i<3; i++) {
            fadeState.currentColor[i] = Math.round(fadeState.currentColor[i] + fadeState.delta[i]);
        }
        // transparency is a float; must not round
        fadeState.currentColor[3] = fadeState.currentColor[3] + fadeState.delta[3];
        rgb = rgbaToString(fadeState.currentColor);
        element.style[property] = rgb;
        fadeState.step++;
    }
    else {
        clearInterval(fadeState.timer);
        rgb = rgbaToString(fadeState.endColor);
        element.style[property] = rgb;
        delete element.fadeState;
    }
}


function colorFade(id,colorProperty,start,end,nSteps,interval) {
    var delta = [], i,rgb, startColor,
        element = document.getElementById(id),
        fadeState = element.fadeState || {};
    nSteps = nSteps || 20;
    interval = interval || 20;
    if (fadeState.timer) {
        clearInterval(fadeState.timer);
    }
    element.fadeState = fadeState;
    startColor = hexToRgbaArray(start);
    fadeState.endColor = hexToRgbaArray(end);
    for (i=0; i<4; i++){
        delta[i] = (fadeState.endColor[i]-startColor[i])/nSteps;
    }

    element.style[colorProperty] = rgbaToString(startColor);
    fadeState.currentColor = startColor;
    fadeState.delta = delta;
    fadeState.step = 1; // init
    fadeState.nSteps = nSteps;
    fadeState.timer = setInterval( function() {
        animateColor(element,colorProperty);
    }, interval);
}

Here's a color fader , creative commons license.
http://www.scriptiny.com/2008/05/javascript-color-fading-script/

I improved on that code to support transparency.

working demo:
http://jsbin.com/eceriv

does not require jquery, or mootools, or any other framework.

The interesting part in the code is like this:

// incrementally close the gap between the two colors
function animateColor(element,property) {
    var i, rgb, fadeState = element.fadeState;
    if (fadeState.step <= fadeState.nSteps) {
        for (i=0; i<3; i++) {
            fadeState.currentColor[i] = Math.round(fadeState.currentColor[i] + fadeState.delta[i]);
        }
        // transparency is a float; must not round
        fadeState.currentColor[3] = fadeState.currentColor[3] + fadeState.delta[3];
        rgb = rgbaToString(fadeState.currentColor);
        element.style[property] = rgb;
        fadeState.step++;
    }
    else {
        clearInterval(fadeState.timer);
        rgb = rgbaToString(fadeState.endColor);
        element.style[property] = rgb;
        delete element.fadeState;
    }
}


function colorFade(id,colorProperty,start,end,nSteps,interval) {
    var delta = [], i,rgb, startColor,
        element = document.getElementById(id),
        fadeState = element.fadeState || {};
    nSteps = nSteps || 20;
    interval = interval || 20;
    if (fadeState.timer) {
        clearInterval(fadeState.timer);
    }
    element.fadeState = fadeState;
    startColor = hexToRgbaArray(start);
    fadeState.endColor = hexToRgbaArray(end);
    for (i=0; i<4; i++){
        delta[i] = (fadeState.endColor[i]-startColor[i])/nSteps;
    }

    element.style[colorProperty] = rgbaToString(startColor);
    fadeState.currentColor = startColor;
    fadeState.delta = delta;
    fadeState.step = 1; // init
    fadeState.nSteps = nSteps;
    fadeState.timer = setInterval( function() {
        animateColor(element,colorProperty);
    }, interval);
}
仙女山的月亮 2024-12-15 20:58:36

您可以使用这个 Jquery 彩色动画插件来做到这一点。当然,这是假设您使用的是 Jquery。如果您的 JavaScript 技能没有您想要的那么强,那么 jQuery 是一个很好的起点。不要误会我的意思,它不能替代学习纯 JavaScript,但它可以为你做很多事情。

彩色动画基于 John Resig 的彩色动画插件,但添加了 rgba 支持,因此您可以获得透明度。您还可以设置文本和边框颜色的动画。

为了从查询字符串中获取照片ID,您可以使用这样的函数(我在SO 此处),但我个人发现,当我想在 namedef(默认)参数很有用。代码> 不是在查询字符串中找到:

function getParameterByName(name, def) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)",
        regex = new RegExp(regexS),
        results = regex.exec(window.location.href);

    if(results == null)
        return def;
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

将其放在脚本标记中的任何位置。然后,要获取参数并刷新它,请将其放置在您需要的位置(例如,head 标记)。在这里,我假设您想在 documentReady 时执行此操作(当加载页面的 DOM 元素时),但您也可以稍微延迟一下,或者等到悬停或其他一些事件。:

$(document).ready(function() {
    var flashDiv = getParameterByName("photoID", false);

    if(flashDiv!==false) {
        $("#"+flashDiv).animate({backgroundColor: '#19426E'}, 2000);
        $("#"+flashDiv).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
    }
});

如果您想延迟 2页面加载后几秒:

$(document).ready(function() {
    var flashDiv = getParameterByName("photoID", false);

    if(flashDiv!==false) {
        setTimeout(function() {
            $("#"+flashDiv).animate({backgroundColor: '#19426E'}, 2000);
            $("#"+flashDiv).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
        }, 2000);
    }
});

如果您想等到用户将鼠标悬停在其上(但仅一次):

$(document).ready(function() {
    var flashDiv = getParameterByName("photoID", false);

    if(flashDiv!==false && !flashed) {
        $("#"+flashDiv).one("mouseover", function() {
            $(this).animate({backgroundColor: '#19426E'}, 2000);
            $(this).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
        });
    }
});

评论后更新:

在 # 之后获取您的 photoId 甚至更容易(您赢了)不需要当然是 getParameterByName 函数):

$(document).ready(function() {
    var photoId = document.location.href.split("#")[1];

    if(photoId!==undefined) {
        $("#"+photoId).animate({backgroundColor: '#19426E'}, 2000);
        $("#"+photoId).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
    }
});

You can do that using this Jquery color animation plugin. Of course, that is assuming that you are using Jquery. If your javascript skills are not as strong as you'd like, jQuery is a good place to start. Don't get me wrong, it's no replacement for learning pure javascript, but it does a lot of things for you.

The color animation based on John Resig's color animation plugin, but adds rgba support so you can have your transparency. You can also animate text and border colors.

In order to get the photo id from the query string, you can use a function like this (which I found in SO here), but I personally find a def (default) argument helpful when I want to set the return value automatically when name is not found in the query string:

function getParameterByName(name, def) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)",
        regex = new RegExp(regexS),
        results = regex.exec(window.location.href);

    if(results == null)
        return def;
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

Put that just about anywhere in a script tag. Then to get your parameter and flash it, place it where you need it (eg., head tag). Here I'm assuming you want to do this at documentReady (when the page's DOM elements are loaded), but you could also delay it a bit, or wait til hover or some other event.:

$(document).ready(function() {
    var flashDiv = getParameterByName("photoID", false);

    if(flashDiv!==false) {
        $("#"+flashDiv).animate({backgroundColor: '#19426E'}, 2000);
        $("#"+flashDiv).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
    }
});

If you'd like to delay that 2 seconds after the page has loaded:

$(document).ready(function() {
    var flashDiv = getParameterByName("photoID", false);

    if(flashDiv!==false) {
        setTimeout(function() {
            $("#"+flashDiv).animate({backgroundColor: '#19426E'}, 2000);
            $("#"+flashDiv).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
        }, 2000);
    }
});

And if you'd like to wait until the user mouses over that (but only once):

$(document).ready(function() {
    var flashDiv = getParameterByName("photoID", false);

    if(flashDiv!==false && !flashed) {
        $("#"+flashDiv).one("mouseover", function() {
            $(this).animate({backgroundColor: '#19426E'}, 2000);
            $(this).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
        });
    }
});

Update after comment:

Getting your photoId after the # is even easier (you won't be needing the getParameterByName function, of course):

$(document).ready(function() {
    var photoId = document.location.href.split("#")[1];

    if(photoId!==undefined) {
        $("#"+photoId).animate({backgroundColor: '#19426E'}, 2000);
        $("#"+photoId).animate({backgroundColor: 'rgba(0,0,0,0)'}, 2000);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文