I have a this function

我有一个这个函数。

function toDiv() {
$(".wrap"))) 

    $('.barfill').each(function(){
        var width=$(this).data('width');
        $(this).animate({ width: width }, 500);
        var $perctext = $('<div>', {'class': 'perctext', text: width});
        $perctext.appendTo(this);
        $perctext.delay(500).each(function(a) {
            $(this).delay(a * 250).animate({
            'left': width
        }, 1000);
    });
}); 
else {}
}

that runs after the element appears on the pane by using this.

在元素出现在窗格之后运行。

function toView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();
    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();
    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

I want it to run ONCE and when the element is on the pane again the function won't trigger again.

我希望它运行一次,当元素在面板上时,函数不会再次触发。

I applied .one() to the parent function so it runs only once but that didn't do the trick.

我对父函数应用了。1()所以它只运行了一次,但这并没有起到什么作用。

You can check the Fiddle Demo to clarify the problem further.

你可以检查演示小提琴进一步澄清问题。



Note: I updated this question for clarity, since it seemed to create some misunderstandings.

注意:为了清晰起见,我更新了这个问题,因为它似乎产生了一些误解。

3 个解决方案

#1


5

Use .unbind() to remove the listener once it's been used:

使用.unbind()移除监听器时,一旦它被使用:

$(window).scroll(toDiv);

function toView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();
    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();
    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

function toDiv() {

    if (toView(
    $(".wrap"))) {
        $('.barfill').each(function () {
            var width = $(this).data('width');
            $(this).animate({
                width: width
            }, 500);
            var $perctext = $('<div>', {
                'class': 'perctext',
                text: width
            });
            $perctext.appendTo(this);
            $perctext.delay(500).each(function (a) {
                $(this).delay(a * 250).animate({
                    'left': width
                }, 1000);
            });
        });
      $(window).unbind('scroll');
    }
}

JSFiddle

JSFiddle

更多相关文章

  1. CSS3(jQUery?)当它悬停时隐藏元素“a”,这样就可以显示元素“b”
  2. Javascript日期/时间函数是否依赖于客户端机器?
  3. 是否可以从节点js中的同一模块导出构造函数和一些正常函数?
  4. javaES6箭头函数的全新特性
  5. JavaScript学习-5——异步同步、回调函数
  6. 停止鼠标用javascript双击某些元素
  7. 什么“返回此”在javascript函数中做什么?
  8. 在量角器中检索子元素的数组
  9. jQuery和AJAX - 使用Ajax添加的对象动态不适用于jQuery函数?

随机推荐

  1. Android权限问题:Permission is only gra
  2. Android跨进程通信IPC之19——AIDL
  3. android 垂直 SeekBar 源代码(VerticalSe
  4. Android(安卓)DownloadManager 的使用
  5. Android(安卓)不得不知Tag的妙用
  6. Android之Intent(一)
  7. Android中捕捉menu按键点击事件的方法
  8. Android包管理机制(三)PMS处理APK的安装
  9. android电量优化之Battery Historian工具
  10. android中如何通过代码来开启和关闭移动