0

我有一个用于固定侧边栏的 jquery 函数。但我只想使用桌面的滚动功能。所以如果它是在手机上打开的,我希望他什么也不做。当我在桌面上时,我希望他打印此代码:

<script type="text/javascript">
    $(function() {
        var offset = $("#sidebar").offset();
        var topPadding = 15;
        $(window).scroll(function() {
            if ($(window).scrollTop() > offset.top) {
                $("#sidebar").stop().animate({
                    marginTop: $(window).scrollTop() - offset.top + topPadding
                },200);
            } else {
                $("#sidebar").stop().animate({
                    marginTop: 0
                      },200);
            };
        });
    });

</script>

我试图这样做,但这没有用。那么当您向下滚动页面时,它将永远不会与您同行。

    <script>
var isMobile = {
Android: function() {
    return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
    return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
    return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
    return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};

if(isMobile.any()){
document.write('')
} else {
 document.write('<script type="text/javascript">
    $(function() {
        var offset = $("#sidebar").offset();
        var topPadding = 15;
        $(window).scroll(function() {
            if ($(window).scrollTop() > offset.top) {
                $("#sidebar").stop().animate({
                    marginTop: $(window).scrollTop() - offset.top + topPadding
                },200);
            } else {
                $("#sidebar").stop().animate({
                    marginTop: 0
                      },200);
            };
        });
    });

</script>')
}</script>
4

0 回答 0