jQuery - 在一个会话中仅运行一次动画
jQuery - Runs Animation Only Once In a Session
我正在使用以下函数在页面首次加载时启动动画。而且它工作正常。
function checkAnimation_ss() {
var $elem = $('.ss');
// If the animation has already been started
if ($elem.hasClass('icon_start')) return; //or same session??
if (isElementInViewport($elem)) {
// Start the animation
$elem.addClass('icon_start');
}
}
我的问题是,当我导航到另一个页面然后点击浏览器的"后退"但键时,动画会再次开始。
有没有办法让动画在一个会话中只运行一次?我知道我需要使用jquery.cookie.js在某种程度上,我只是想不通?
非常感谢您的帮助
是的,您可以将cookie与jquery.cookie一起使用.js在这种情况下,您需要执行类似于以下内容的操作:
function checkAnimation_ss() {
var cookieValue = $.cookie("animation");
if(cookieValue == 1)
{}
else
{
var $elem = $('.ss');
// If the animation has already been started
if ($elem.hasClass('icon_start')) return; //or same session??
if (isElementInViewport($elem)) {
// Start the animation
$elem.addClass('icon_start');
$.cookie("animation", 1);
}
}}
当然,如果您的网站使用它,您也可以使用 php 解决它。在这种情况下,您可以执行以下操作:
<?php session_start(); $_SESSION['animation'] = 1; ?>
然后:
function checkAnimation_ss() {
<?php
if($_SESSION['animation'] == 1)
{}
else
{
?>
var $elem = $('.ss');
// If the animation has already been started
if ($elem.hasClass('icon_start')) return; //or same session??
if (isElementInViewport($elem)) {
// Start the animation
$elem.addClass('icon_start');
$.cookie("animation", 1);
}
<?php
}
?>}
最终代码 ...
function checkAnimation_ss() {
var cookieValue = $.cookie("animation");
var $elem = $('.ss');
if(cookieValue == 1){
$elem.addClass('icon_visible');
}else{
// If the animation has already been started
if ($elem.hasClass('icon_start')) return;
if (isElementInViewport($elem)) {
// Start the animation
$elem.addClass('icon_start');
$.cookie("animation", 1);
}
}
}
相关文章:
- 动画、计时和运行循环等问题
- HTML5(Bootstrap)通过函数调用运行动画
- 为什么我的脚本正在运行动画,但没有显示它
- 画布动画似乎不会以60帧/秒的速度运行
- 仅在尚未运行jQuery UI的情况下运行动画
- Jquery toggleClass toggle的类,但不会多次运行动画
- 如何在另一个动画正在进行时延迟运行动画
- 运行动画,然后删除元素
- 如何通过Javascript运行动画
- Span元素不从jQuery运行动画
- 点击浏览器返回按钮运行动画
- 如何在每次输入变化时运行动画
- 简单的预加载器,加载所有的资源,并在显示网站和运行动画之前缓存它们
- 用Javascript运行动画后删除html标签
- 在jquery中运行动画后激活add类
- 同时运行动画不工作
- jQuery 运行动画,在文档加载时阻止默认值
- Skrollr 运行动画一次,然后停止
- 当这个动画准备好时再次运行动画
- 更好的方式来运行动画的有限循环