Jquery jscrollpane 内的宽度以适应不同的宽度

发布于 2024-11-24 01:55:35 字数 2361 浏览 2 评论 0原文

这个问题与之前回答的问题相关,可以在这里找到:Jquery jscrollpane 宽度根据内容自动调整

感谢 Mac 用户提供了以下解决方案:

JS 应该如下所示:

$(function(){
var totalImages = $("#images img").length;
var imgWidth = 661 + 5; //width of an image plus the margin
$("#imagesContainer").css("width",totalImages*imgWidth - 5);

$('.scroll-pane').jScrollPane({
showArrows: true,
autoReinitialise: true
});
});

CSS 如下:

#imagesContainer {
width:5000px; /*fallback width*/
overflow:hidden;
}

#imagesContainer img {
display:block;
margin:0 5px 0 0; /*adding a 5px margin to the right*/
padding:0;
float:left;
width: 661px;
height: 440px;
}

.lastImage {
margin-right:0 !important; /*removing the margin right from the last img*/
}

最后是 HTML:

<div id="imagesContainer">
<img src="images/food.jpg" />
<img src="images/food.jpg" />
<img src="images/food.jpg" />
<img src="images/food.jpg" />
<img class="lastImage" src="images/food.jpg" />
</div>

这个解决方案解决了宽度jScrollPane 根据内容(在本例中为图像)按需扩展。您可以在此处查看结果: http://www.giamarescotti.com/v2/index.html 您会注意到肖像照片的侧面扭曲。

为了解决这个问题,我尝试为肖像图像创建以下 css:

#imagesContainer img.portrait {
display:block;
margin: 0 5px 0 0; /*adding a 5px margin to the right*/
padding:0;
float:left;
width: 293px;
height: 440px;
}

.lastImagePortrait {
margin-right:0 !important; /*removing the margin right from the last img*/
width: 293px;
height: 440px;
}

现在 HTML 中有以下类:

<img src="images/food_bev/kara07.jpg" />
<img src="images/food_bev/kara08.jpg" />
<img class="portrait" src="images/food_bev/kara09.jpg" />
<img class="portrait" src="images/food_bev/kara10.jpg" />

您可以在此处查看我的尝试: http://www.giamarescotti.com/v2/attempt.html

它解决了扭曲问题,但现在由于 JS 在幻灯片末尾有额外的空间:

    var imgWidth = 661 + 5; //661 applies only to landscape, not portrait

显然是661px 宽度仅适用于横向图像。肖像宽度应为 293 像素。那么如何告诉JS有2个宽度需要计算呢?

非常感谢任何建议。非常感谢!

This question is related to a previous answered question which can be found here: Jquery jscrollpane width to adjust automatically according to content

Thanks to user Mac who has provided the following solution:

JS should look like below:

$(function(){
var totalImages = $("#images img").length;
var imgWidth = 661 + 5; //width of an image plus the margin
$("#imagesContainer").css("width",totalImages*imgWidth - 5);

$('.scroll-pane').jScrollPane({
showArrows: true,
autoReinitialise: true
});
});

CSS as such:

#imagesContainer {
width:5000px; /*fallback width*/
overflow:hidden;
}

#imagesContainer img {
display:block;
margin:0 5px 0 0; /*adding a 5px margin to the right*/
padding:0;
float:left;
width: 661px;
height: 440px;
}

.lastImage {
margin-right:0 !important; /*removing the margin right from the last img*/
}

And finally the HTML:

<div id="imagesContainer">
<img src="images/food.jpg" />
<img src="images/food.jpg" />
<img src="images/food.jpg" />
<img src="images/food.jpg" />
<img class="lastImage" src="images/food.jpg" />
</div>

This solution solved width of jScrollPane expanding as needed according to content (in this case images). You may view the result here: http://www.giamarescotti.com/v2/index.html
You will notice that portrait pictures are distorted sideways.

In my attempt to solve that, I tried creating the following css for portrait images:

#imagesContainer img.portrait {
display:block;
margin: 0 5px 0 0; /*adding a 5px margin to the right*/
padding:0;
float:left;
width: 293px;
height: 440px;
}

.lastImagePortrait {
margin-right:0 !important; /*removing the margin right from the last img*/
width: 293px;
height: 440px;
}

And now have the following class in the HTML:

<img src="images/food_bev/kara07.jpg" />
<img src="images/food_bev/kara08.jpg" />
<img class="portrait" src="images/food_bev/kara09.jpg" />
<img class="portrait" src="images/food_bev/kara10.jpg" />

You may view my attempt here: http://www.giamarescotti.com/v2/attempt.html

It solves the distortion but now has extra space at the end of the slide due to the JS:

    var imgWidth = 661 + 5; //661 applies only to landscape, not portrait

Obviously the 661px width applies only to landscape images. Portrait should have a 293px width. So how do you tell JS that there are 2 widths that needs to be calculated?

Any advice is greatly appreciated. Thank you very much!

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

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

发布评论

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

评论(2

鲜肉鲜肉永远不皱 2024-12-01 01:55:35

好吧,我有 5 分钟的空闲时间,希望能用一些 CSS 魔法来解决整个问题,因为正如我在上次回答你的其他问题时所说,仅 CSS 的方法将是你的情况的最佳方法:)

这意味着您不再需要任何 JavaScript 来计算宽度,这也意味着如果访问者禁用了 JavaScript,这将为他们带来更好的用户体验,并且显然您现在不需要担心扭曲的图像 :)

所以我担心首先要做的事情您将需要删除任何计算宽度等的 JavaScript,您应该只为 JavaScript 留下这个(jscrollpane 代码)。

$(function(){

$('.scroll-pane').jScrollPane({
    showArrows: true,
    autoReinitialise: true
});

});

您的 HTML 需要稍微更改一下,

<div id="images">
    <div class="scroll-pane horizontal-only">
        <ul id="imagesContainer">
            <li><img height="440" src="images/food.jpg" /></li>
            <li><img height="440" src="images/food.jpg" /></li>
            <li><img height="440" src="images/food.jpg" /></li>
            <li><img height="440" src="images/food.jpg" /></li>
            <li><img height="440" class="lastImage" src="images/food.jpg" /></li>
        </ul>
    </div>
</div>

请注意,您需要设置图像的高度(应始终为 440px,因为这是您当前在代码中设置的高度)。这样,如果图像速度很慢,则无法加载滚动区域,这可以解决问题!

我已经设置了 CSS 和滚动区域等来拍摄高度为 440px 的图像,但图像的宽度并不重要,CSS 会处理这个问题! :)

最后,您需要对 CSS 进行一些更改,如下所示:

#images {
    width: 100%;
    margin: 20px auto 40px auto;
}

#imagesContainer {
    list-style:none; 
    display:inline-block; 
    white-space: nowrap; 
    height:auto; 
    margin:0 5px 0 5px; 
    padding:0 0 0 5px;
    zoom:1; /*required for IE 6 and 7*/
    *display: inline; /*required for IE 6 and 7*/
}

#imagesContainer li {
    height:440px;
    width:auto;
    margin:0 5px 0 0;
    padding:0;
    display:inline-block;
    zoom:1; /*required for IE 6 and 7*/
    *display: inline; /*required for IE 6 and 7*/
}

#imagesContainer li img {
    padding:0;
    margin:0;
}

.lastImage {
    margin-right:0 !important; /*removing the margin right from the last img*/
}


.scroll-pane {
    width: 100%;
    height: auto;
    overflow: auto;
}

.horizontal-only {
    height:470px;
}

编辑:在 Safari、Chrome、Firefox、Opera、IE6+ 中测试的浏览器(添加了对 IE6 和 IE7 的支持!)

对,希望如此应该是吧!

Ok so I had a spare 5 minutes and have hopefully done some CSS magic to fix this entire issue, because as I said in my last answer to your other question, a CSS only way is going to be the best way in your case :)

This means you no longer need any JavaScript to calculate your width's which also means that if visitors have JavaScript disabled it will be a much better user experience for them and obviously you now don't need to worry about distorted images :)

So first things first I'm afraid you will need to remove any JavaScript that calculates widths etc, you should be left with just this for your JavaScript (the jscrollpane code).

$(function(){

$('.scroll-pane').jScrollPane({
    showArrows: true,
    autoReinitialise: true
});

});

Your HTML will need slightly changing, to this

<div id="images">
    <div class="scroll-pane horizontal-only">
        <ul id="imagesContainer">
            <li><img height="440" src="images/food.jpg" /></li>
            <li><img height="440" src="images/food.jpg" /></li>
            <li><img height="440" src="images/food.jpg" /></li>
            <li><img height="440" src="images/food.jpg" /></li>
            <li><img height="440" class="lastImage" src="images/food.jpg" /></li>
        </ul>
    </div>
</div>

Please note that you need to set the height of the image (which should always be 440px as that is what you have it currently set to in your code). This is so that if an image is slow, of fails to load the scrolling area has something to work with!

I have setup the CSS and scrolling area etc to take images with a height of 440px, but the width of your images does not matter, the CSS takes care of that! :)

Lastly you need to make some changes to your CSS, to look like this:

#images {
    width: 100%;
    margin: 20px auto 40px auto;
}

#imagesContainer {
    list-style:none; 
    display:inline-block; 
    white-space: nowrap; 
    height:auto; 
    margin:0 5px 0 5px; 
    padding:0 0 0 5px;
    zoom:1; /*required for IE 6 and 7*/
    *display: inline; /*required for IE 6 and 7*/
}

#imagesContainer li {
    height:440px;
    width:auto;
    margin:0 5px 0 0;
    padding:0;
    display:inline-block;
    zoom:1; /*required for IE 6 and 7*/
    *display: inline; /*required for IE 6 and 7*/
}

#imagesContainer li img {
    padding:0;
    margin:0;
}

.lastImage {
    margin-right:0 !important; /*removing the margin right from the last img*/
}


.scroll-pane {
    width: 100%;
    height: auto;
    overflow: auto;
}

.horizontal-only {
    height:470px;
}

EDIT: Browser tested in Safari, Chrome, Firefox, Opera, IE6+ (added in support for IE6 and IE7!)

Right, hopefully that should be it!

只是偏爱你 2024-12-01 01:55:35

在#sourcecode 中尝试一下:

$(function(){
var totalLandscapeImages = $("#images img").length - $("img.portrait").length;
var totalPortraitImages = $("img.portrait").length;
var imgLandscapeWidth = 661+5; //width of a landscape image plus any margins or padding it might have
var imgPortraitWidth = 293+5; //width of a portrait image plus any margins or padding it might have
$("#imagesContainer").css("width",(totalLandscapeImages*imgLandscapeWidth) + (totalPortraitImages*imgPortraitWidth) - 5);

$('.scroll-pane').jScrollPane({
        showArrows: true,
        autoReinitialise: true
        });
});

Try this in #sourcecode:

$(function(){
var totalLandscapeImages = $("#images img").length - $("img.portrait").length;
var totalPortraitImages = $("img.portrait").length;
var imgLandscapeWidth = 661+5; //width of a landscape image plus any margins or padding it might have
var imgPortraitWidth = 293+5; //width of a portrait image plus any margins or padding it might have
$("#imagesContainer").css("width",(totalLandscapeImages*imgLandscapeWidth) + (totalPortraitImages*imgPortraitWidth) - 5);

$('.scroll-pane').jScrollPane({
        showArrows: true,
        autoReinitialise: true
        });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文