来自jsfiddle的代码无法运行。

3

我需要一份在我的网站中使用的代码(我几乎找不到),但当我将它放在我的html文件中时,它无法正常工作。

我尝试把它放在<html>之前,在和中都没有用。
也尝试过 $(document).ready(function() { ... });

我正在使用:

<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>

但也许我需要另一个。

这里是 jsfiddle:http://jsfiddle.net/Tgm6Y/1447/

var windw = this;

$.fn.followTo = function ( elem ) {
    var $this = this,
        $window = $(windw),
        $bumper = $(elem),
        bumperPos = $bumper.offset().top,
        thisHeight = $this.outerHeight(),
        setPosition = function(){
            if ($window.scrollTop() > (bumperPos - thisHeight)) {
                $this.css({
                    position: 'absolute',
                    top: (bumperPos - thisHeight)
                });
            } else {
                $this.css({
                    position: 'fixed',
                    top: 0
                });
            }
        };
    $window.resize(function()
    {
        bumperPos = pos.offset().top;
        thisHeight = $this.outerHeight();
        setPosition();
    });
    $window.scroll(setPosition);
    setPosition();
};

$('#one').followTo('#two');

以上代码有什么问题?你的目标是什么? - divy3993
猜测这与您的CSS位置有关。 - divy3993
2个回答

1

这段代码在文档准备函数中可以正常工作。你的JavaScript可能出错了,因为ajax脚本是404错误。我刚刚测试了这段代码,它可以完美运行:

<html>
<head>
<style>
body, html{
    height:200%;
}


#one {
    width:100%;    
    height: 200px;
    background-color: aqua;
    position: fixed;
}


#two {
    width: 100%;    
    height:50px;
    background-color: red;
    margin-top:150%;
    position:absolute;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script>
$(document).ready(function() {
    var windw = this;

    $.fn.followTo = function ( elem ) {
        var $this = this,
            $window = $(windw),
            $bumper = $(elem),
            bumperPos = $bumper.offset().top,
            thisHeight = $this.outerHeight(),
            setPosition = function(){
                if ($window.scrollTop() > (bumperPos - thisHeight)) {
                    $this.css({
                        position: 'absolute',
                        top: (bumperPos - thisHeight)
                    });
                } else {
                    $this.css({
                        position: 'fixed',
                        top: 0
                    });
                }
            };
        $window.resize(function()
        {
            bumperPos = pos.offset().top;
            thisHeight = $this.outerHeight();
            setPosition();
        });
        $window.scroll(setPosition);
        setPosition();
    };

    $('#one').followTo('#two');
});
</script>
</head>
<body>
    <div id="one">FIXED...</div>
    <div id="two">...BUT STOPS HERE</div>
</body>
</html>

如果仍然无法正常工作,请检查浏览器中的控制台,可能只是您的JavaScript中存在随机语法错误。将此代码粘贴到文件中将演示与jsfiddle相同的事情。

复制并且可以运行,但是当我在代码布局中添加文档准备时它就无法工作了。不管怎样,非常感谢。 - Dennis Novac
@DeneaNovac请在浏览器的控制台检查,很可能只是某个地方出现了语法错误。 - Kevin DiTraglia

0

如果你想让这个JSFiddle工作正常,那么它已经可以正常工作了。但是,如果你在这里使用的任何代码中缺少引号@第二个JS文件路径,或者如果你想要其他更多的东西,除了这个fiddle,你能否在这里提一下...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

以下代码运行良好

<!DOCTYPE html>
<html>
<head>
    <style>
       body, html{
        height:200%;
        }


        #one {
            width:100%;    
            height: 200px;
            background-color: aqua;
            position: fixed;
        }


        #two {
            width: 100%;    
            height:50px;
            background-color: red;
            margin-top:150%;
            position:absolute;
        }
    </style>
    <script src="http://code.jquery.com/jquery-1.11.0.js"></script>
    <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> -->
</head>
<body>
    <div id="one">FIXED...</div>
    <div id="two">...BUT STOPS HERE</div>
    <script>
        var windw = this;

        $.fn.followTo = function ( elem ) {
            var $this = this,
                $window = $(windw),
                $bumper = $(elem),
                bumperPos = $bumper.offset().top,
                thisHeight = $this.outerHeight(),
                setPosition = function(){
                    if ($window.scrollTop() > (bumperPos - thisHeight)) {
                        $this.css({
                            position: 'absolute',
                            top: (bumperPos - thisHeight)
                        });
                    } else {
                        $this.css({
                            position: 'fixed',
                            top: 0
                        });
                    }
                };
            $window.resize(function()
            {
                bumperPos = pos.offset().top;
                thisHeight = $this.outerHeight();
                setPosition();
            });
            $window.scroll(setPosition);
            setPosition();
        };

        $('#one').followTo('#two');


    </script>
</body>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接