写jq插件时遇到问题

Uncaught TypeError: Cannot use 'in' operator to search for 'marginTop' in undefined
这个提示是什么意思?
我写的插件包含一个动画效果marginTop,但是却不能设置,为什么

//代码

(function($){

$.fn.newsScroll = function(options){
    var dft = {
        'speed': '600',  //滚动速度
        'time' : '3000'  //每次滚动的时间
    };
    var opts = $.extend(dft, options);
    var setTime;
    var scrollSpeed = opts.speed;
    var scrollTime = opts.titme;
    function newsScroll(){
        var newsHeight = $(this).children().eq(0).height();  //获取父元素第一个子元素的高度,为父元素向上移动做准备;
        $(this).animate({ "marginTop": -newsHeight + "px"}, scrollSpeed, function() {
            $(this).css("marginTop", "0").children().eq(0).appendTo($(this));  //设置上边距为零,为了下一次移动做准备
        });
    }
    $(this).hover(
        function () {
            clearInterval(setTime);
        },
        function(){
            setTime = setInterval(newsScroll,scrollTime);
        }
    ).trigger("mouseleave");
}
})(jQuery)
阅读 2.9k
3 个回答

newscroll的指向是window。你可以输出this看看

你的newScroll中的this指向不再和$.fn.newsScroll = function(options){}中的this指向相同。

var that = this;
function newsScroll(){
        var newsHeight = $(that).children().eq(0).height();  //获取父元素第一个子元素的高度,为父元素向上移动做准备;
        $(that).animate({ "marginTop": -newsHeight + "px"}, scrollSpeed, function() {
            $(that).css("marginTop", "0").children().eq(0).appendTo($(that));  //设置上边距为零,为了下一次移动做准备
        });
    }

newsScroll中this的指向改变了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题