适用于移动网站的 jQuery 可滚动 Div

发布于 2024-10-03 23:38:43 字数 1356 浏览 1 评论 0原文

我正在尝试为移动网站创建一个 jQuery 插件,将 div 容器变成可滚动容器。我通过将 div 容器内的所有子级包装在另一个绝对定位的 div 容器中来实现此目的。然后我修改内部 div 的顶部或左侧 css 样式以模拟上下或左右移动文本。我使用触摸事件侦听器来确定何时移动滚动 div。

但是,我在设置内部滚动 div 的 width() 时遇到一些问题。这是代码示例:

myObj.ready(function(){
myObj.css({ 'position' : 'relative', 'overflow' : 'hidden' }).wrapInner('<div id="scrollCntr">');
$('#scrollCntr').prepend("<img src='scrollBar.png' id='scrollBar' class='horizontal'");
$('#scrollCntr').height( myObj.height() );

if ( config.width <= 0 ) {
    $('#scrollCntr').children().each(function(){
        if ( $(this).css('display') == 'inline' || $(this).css('float') == 'left' || $(this).css('float') == 'right' ) {
            totalWidth += $(this).width();

            for( var i = 0; i < boxProps.length; i++ )
                if ( $(this).css(boxProps[i]) != "" )
                    totalWidth += parseInt( $(this).css(boxProps[i]).replace( /px/i, '' ).replace( /em/i, '' ).replace( /pt/i, '' ) );
        }
    });
} else {
    totalWidth = config.width;
}

console.log( totalWidth );
$('#scrollCntr').width( totalWidth );
console.log( $('#scrollCntr').width() );

});

在本例中,config.width 设置为 719,myObj 是正在滚动的容器 div。我不太确定问题是什么。在我的演示中,当我的子元素只是图像时,我没有遇到任何问题,但是当我将其应用到我的移动网站时,其中子元素是 div 框,它就出现了问题。也许子元素没有及时加载?但这并不能解释为什么我无法设置滚动 div 的宽度。它肯定正在加载。我不知道问题是什么......

I'm trying to create a jQuery plugin for a mobile website that turns a div container into a scrollable container. I accomplish this by wrapping all the children that are inside the div container in another div container thats absolutely positioned. Then I modify the top or left css styles of the inside div to simulate moving the text up and down or left and right. I'm using touch event listeners to determine when to move the scrolling div.

However, I'm having some trouble setting the width() of my inside scrolling div. Here's a sample of the code:

myObj.ready(function(){
myObj.css({ 'position' : 'relative', 'overflow' : 'hidden' }).wrapInner('<div id="scrollCntr">');
$('#scrollCntr').prepend("<img src='scrollBar.png' id='scrollBar' class='horizontal'");
$('#scrollCntr').height( myObj.height() );

if ( config.width <= 0 ) {
    $('#scrollCntr').children().each(function(){
        if ( $(this).css('display') == 'inline' || $(this).css('float') == 'left' || $(this).css('float') == 'right' ) {
            totalWidth += $(this).width();

            for( var i = 0; i < boxProps.length; i++ )
                if ( $(this).css(boxProps[i]) != "" )
                    totalWidth += parseInt( $(this).css(boxProps[i]).replace( /px/i, '' ).replace( /em/i, '' ).replace( /pt/i, '' ) );
        }
    });
} else {
    totalWidth = config.width;
}

console.log( totalWidth );
$('#scrollCntr').width( totalWidth );
console.log( $('#scrollCntr').width() );

});

In this case, config.width is set to 719 and myObj is the container div being scrolled. I'm not really sure what the problem is. I didn't have any problems with this in my demo, when my children elements where just images, but when I applied it to my mobile website, where the children elements where div boxes, its been having problems. Perhaps the children elements aren't being loaded in time? But that wouldn't explain why I can't set the scrolling div's width. Its definately being loaded. I have no idea what the problem is...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2024-10-10 23:38:43

知道了。在另一个问题上找到了它:

要触发的 jQuery 事件当 div 可见时执行的操作

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文