1

我对 jQuery/JS 非常陌生,并且在特定窗口大小中添加或删除功能几乎没有问题。如果您可以查看我的代码并进行更正,我将不胜感激。

详细地说,我使用了 tinyscrollbar 插件,我只希望它出现在特定的窗口大小中(假设窗口宽度超过 650 像素)。

这是我的代码:

<!--****preload latest jQuery and tinyscrollbar plugin, then...****-->

<script type="text/javascript">

function newsScroll() {
    $("#newsScroll").tinyscrollbar();
};

/*
if windows width is less than 650px, remove newsScroll function and
switch DIV ID to "spNewsScroll" and vice versa.
*/
function scrollOnOff(width) {
    width = parseInt(width);
    if (width < 650) {
        $(window).unbind(newsScroll);
        $("#newsScroll").attr('id','spNewsScroll');
    } else {
        $(window).bind(newsScroll);
        $("#spNewsScroll").attr('id','newsScroll');
    }
};

$(function() {
    //get window size as of now.
    scrollOnOff($(this).width());
    $(window).resize(function() {
        scrollOnOff($(this).width());
    });       
});

</script>
4

1 回答 1

2

试试这个,经过测试和工作:-)

现场演示:http: //jsfiddle.net/oscarj24/fUsnj/21/

function scrollOnOff(width) {
    width = parseInt(width);
    if(width < 650){
        $(window).trigger('newsScroll');
        $('#newsScroll').attr('id','spNewsScroll');
    } else {
        $('.scrollbar').hide();
        $('#spNewsScroll').attr('id','newsScroll');
    }
};

$(function() {

    $(window).bind('newsScroll', function(e) {
        $('.scrollbar').show();
        $('#newsScroll').tinyscrollbar();
    });

    var screenWidth = $(this).width();
    scrollOnOff(screenWidth);

    $(window).on("resize", function(event){
        var w = $(this).width();
        scrollOnOff(w);                
    });

});

​</p>

于 2012-05-23T02:15:10.577 回答