为什么这个javascript无法在我的网站上运行?

时间:2014-04-10 10:04:30

标签: javascript jquery html css

我确实看到红色的突发新闻,但文字不在正确的位置,我看到了这个词:你好,并且没有动作,文字是静态的。

这是我在我网站的html代码中使用的代码:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
<script language='JavaScript'>
var oneliner = $('#oneliner .newsticker').newsTicker({
    row_height: 44,
    max_rows: 1,
    time: 5000,
    nextButton: $('#oneliner .header')
});
$('#oneliner .header').hover(function() {
    oneliner.newsTicker('pause');
}, function() {
    oneliner.newsTicker('unpause');
});
</script>


<div id="oneliner">
    <div class="header"> Breaking News </div>
    <ul class="newsticker">
        <li>hello</li>
        <li>hi</li>
    </ul>
</div>


<style>
#oneliner {
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
    position: relative;
    margin: 80px auto 40px auto;
    width: 900px;
    height: 44px;
    background-color: #fff;
    border-radius: 2px;
}

#oneliner:before, #oneliner:after {
    border-radius: 100px 100px 100px 100px / 10px 10px 10px 10px;
    bottom: 0;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    content: "";
    left: 10px;
    position: absolute;
    right: 10px;
    top: 50%;
    z-index: -1;
}

#oneliner .header {
    background-color: #ff2e2e;
    display: inline-block;
    width: 180px;
    height: 44px;
    text-transform: uppercase;
    font-weight: 500;
    line-height: 44px;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-align: center;
    border-radius: 2px 0 0 2px;
    cursor: pointer;
    -webkit-transition: all .2s ease-in-out;
       -moz-transition: all .2s ease-in-out;
        -ms-transition: all .2s ease-in-out;
         -o-transition: all .2s ease-in-out;
            transition: all .2s ease-in-out;
    -webkit-animation: redPulse 4s infinite;
       -moz-animation: redPulse 4s infinite;
            animation: redPulse 4s infinite;
}

#oneliner:hover .header {
    -webkit-animation: redPulse 1s infinite;
       -moz-animation: redPulse 1s infinite;
            animation: redPulse 1s infinite;
}

#oneliner .header:active {
    background-color: #000;
    margin-left: -12px;
    color: #aaa;
}

#oneliner ul.newsticker {
    display: inline-block;
    height: 44px;
    width: 710px;
    overflow: hidden;
    margin: 0 0 0 -3px;
    padding: 0 0 0 6px;
    line-height: 44px;
    font-weight: 500;
    background-color: #fafafa;
}

#oneliner ul.newsticker > li{
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    border-bottom: 1px dotted #888;
}

@-webkit-keyframes redPulse {
    from { background-color: #ff2e2e; -webkit-box-shadow: 0 0 9px #555; }
    50% { background-color: #bd0000; -webkit-box-shadow: 0 0 14px #ff2e2e; }
    to { background-color: #ff2e2e; -webkit-box-shadow: 0 0 9px #555; }
}

@-moz-keyframes redPulse {
    from { background-color: #ff2e2e; -moz-box-shadow: 0 0 9px #555; }
    50% { background-color: #bd0000; -moz-box-shadow: 0 0 14px #ff2e2e; }
    to { background-color: #ff2e2e; -moz-box-shadow: 0 0 9px #555; }
}

@keyframes redPulse {
   from { background-color: #ff2e2e; box-shadow: 0 0 9px #555; }
   50% { background-color: #bd0000; box-shadow: 0 0 14px #ff2e2e; }
   to { background-color: #ff2e2e; box-shadow: 0 0 9px #555; }
}
</style>

这就是它应该如何在演示页面中使用突发新闻的第一个例子:

http://www.jqueryscript.net/demo/Flexible-jQuery-Vertical-News-Ticker-Plugin-Advanced-News-Ticker/

我现在看到的只是在演示中的突发新闻,但是hello这个词在它之下而不在其中而不是滚动。

编辑:

更改了代码顺序,所以我看到了Breaking News但DIV标签里面的单词我只看到了突发新闻下的灰色字,你没有动,也没有像在演示页面那样突发新闻。我还下载了文件http://www.jqueryscript.net/animation/Flexible-jQuery-Vertical-News-Ticker-Plugin-Advanced-News-Ticker.html

我现在该怎么办呢?如果我使用它们在我的javascript中调用它们,将它们放到我的网站上?

<style>
#oneliner {
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
    position: relative;
    margin: 80px auto 40px auto;
    width: 900px;
    height: 44px;
    background-color: #fff;
    border-radius: 2px;
}

#oneliner:before, #oneliner:after {
    border-radius: 100px 100px 100px 100px / 10px 10px 10px 10px;
    bottom: 0;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    content: "";
    left: 10px;
    position: absolute;
    right: 10px;
    top: 50%;
    z-index: -1;
}

#oneliner .header {
    background-color: #ff2e2e;
    display: inline-block;
    width: 180px;
    height: 44px;
    text-transform: uppercase;
    font-weight: 500;
    line-height: 44px;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-align: center;
    border-radius: 2px 0 0 2px;
    cursor: pointer;
    -webkit-transition: all .2s ease-in-out;
       -moz-transition: all .2s ease-in-out;
        -ms-transition: all .2s ease-in-out;
         -o-transition: all .2s ease-in-out;
            transition: all .2s ease-in-out;
    -webkit-animation: redPulse 4s infinite;
       -moz-animation: redPulse 4s infinite;
            animation: redPulse 4s infinite;
}

#oneliner:hover .header {
    -webkit-animation: redPulse 1s infinite;
       -moz-animation: redPulse 1s infinite;
            animation: redPulse 1s infinite;
}

#oneliner .header:active {
    background-color: #000;
    margin-left: -12px;
    color: #aaa;
}

#oneliner ul.newsticker {
    display: inline-block;
    height: 44px;
    width: 710px;
    overflow: hidden;
    margin: 0 0 0 -3px;
    padding: 0 0 0 6px;
    line-height: 44px;
    font-weight: 500;
    background-color: #fafafa;
}

#oneliner ul.newsticker > li{
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    border-bottom: 1px dotted #888;
}

@-webkit-keyframes redPulse {
    from { background-color: #ff2e2e; -webkit-box-shadow: 0 0 9px #555; }
    50% { background-color: #bd0000; -webkit-box-shadow: 0 0 14px #ff2e2e; }
    to { background-color: #ff2e2e; -webkit-box-shadow: 0 0 9px #555; }
}

@-moz-keyframes redPulse {
    from { background-color: #ff2e2e; -moz-box-shadow: 0 0 9px #555; }
    50% { background-color: #bd0000; -moz-box-shadow: 0 0 14px #ff2e2e; }
    to { background-color: #ff2e2e; -moz-box-shadow: 0 0 9px #555; }
}

@keyframes redPulse {
   from { background-color: #ff2e2e; box-shadow: 0 0 9px #555; }
   50% { background-color: #bd0000; box-shadow: 0 0 14px #ff2e2e; }
   to { background-color: #ff2e2e; box-shadow: 0 0 9px #555; }
}
</style>

<div id="oneliner">
    <div class="header"> Breaking News </div>
    <ul class="newsticker">
        <li>hello</li>
        <li>hi</li>
    </ul>
</div>


<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
<script language='JavaScript'>
$( document ).ready(function() {
           var oneliner = $('#oneliner .newsticker').newsTicker({
               row_height: 44,
               max_rows: 1,
               time: 5000,
               nextButton: $('#oneliner .header')
           });
           $('#oneliner .header').hover(function() {
               oneliner.newsTicker('pause');
           }, function() {
               oneliner.newsTicker('unpause');
           });
    });
</script>

4 个答案:

答案 0 :(得分:0)

您尝试选择的元素显示在<script>之后。当您尝试选择它们时,它们不存在。

将脚本移动到文档的末尾或将其转换为函数,并在ready事件触发时调用该函数(jQuery(yourFunction);)。

答案 1 :(得分:0)

尝试这个我认为这会对你有所帮助。

<script language='JavaScript'>
        $(document).ready(function()
        var oneliner = $('#oneliner .newsticker').newsTicker({
            row_height: 44,
            max_rows: 1,
            time: 5000,
            nextButton: $('#oneliner .header')
        });
        $('#oneliner .header').hover(function() {
            oneliner.newsTicker('pause');
        }, function() {
            oneliner.newsTicker('unpause');
        });
    });
        </script>

答案 2 :(得分:0)

&#39; newsTicker&#39;方法没有在基本的jQuery库中定义。

您需要下载Flexible-jQuery-Vertical-News-Ticker-Plugin库。转到以下链接:

http://www.jqueryscript.net/animation/Flexible-jQuery-Vertical-News-Ticker-Plugin-Advanced-News-Ticker.html

答案 3 :(得分:0)

将您的脚本放在jQuery中的onReady函数中。这将确保在您尝试访问元素之前已加载页面: http://learn.jquery.com/using-jquery-core/document-ready/

    $( document ).ready(function() {
           var oneliner = $('#oneliner .newsticker').newsTicker({
               row_height: 44,
               max_rows: 1,
               time: 5000,
               nextButton: $('#oneliner .header')
           });
           $('#oneliner .header').hover(function() {
               oneliner.newsTicker('pause');
           }, function() {
               oneliner.newsTicker('unpause');
           });
    });