1

私はjQuery/JSを初めて使用し、特定のウィンドウサイズで機能を追加または削除することにほとんど問題はありません。私のコードを見て修正していただければ幸いです。

詳細には、特定のウィンドウ サイズ (ウィンドウ幅が 650 ピクセルを超えるとしましょう) にのみ表示する tinyscrollbar プラグインを使用します。

これが私のコードです:

<!--****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 に答える