ここで実装を表示できます。
したがって、これが行うことは、ウィンドウのサイズを自動的に検出し、オンザフライで表示される画像のサイズを変更することです。つまり、すべての画面解像度で比率が同じであるという考えです。
よりエレガントで「スムーズ」にするための解決策が欲しいという私が見つけたいくつかの問題を次に示します。
- より大きな解像度 (たとえば 1280+) では見た目も機能も問題ありませんが、1024 以下で読み込もうとすると、見た目も機能もおかしくなり始めます。
- スクロール中はスムーズにスクロールしません。
- 配置の一部がオフになっています (つまり、'3-up' または '4-up' ビューに切り替えると、1680 解像度で一番右の画像の白い境界線が消えます)。
- 4-up に移動して右端までスクロールすると、画像が表示されずに奇妙に終了します。
- DRYではないことを私が知っている繰り返しのJSがあり、現在のバージョンが複数の「ビュー」に対して行うことを達成するためのよりエレガントな方法が大好きです。
- 最初に読み込まれると、読み込みに約 3 秒かかります。その間、実際に小さいバージョンが表示され、サイズ変更された画像がページに配置されるタイミングが表示されます。それは私には少し「乱雑」に思えるので、もっときれいにしてほしいです。
- さらに、それを「引き締め」て、全体的に機能を向上させる方法に関するヒント。
そのフィードバック ページでソースを表示することもできますが、無関係なコードがそこにあるため、関連する部分を次に示します。
HTML:
<div id="compare_view" align="center">
<div id="viewbar" class="compv-navbar">
<a href=""><img src="images/2-up-icon-grey.png" alt="2-up-view" data-id="2"></a> |
<a href=""><img src="images/3-up-icon-grey.png" alt="3-up-view" data-id="3"></a> |
<a href=""><img src="images/4-up-icon-grey.png" alt="4-up-view" data-id="4"></a> |
<span id="viewname" class="view_name">2-up</span>
</div>
<div id="slider-code" align="center">
<a class="buttons prev" href="#"></a>
<div class="viewport">
<ul class="overview">
<li><img src="images/red-stripe.jpg" /></li>
<li><img src="images/red-stripe-bw.jpg" /></li>
<li><img src="images/red-stripe-red.jpg" /></li>
<li><img src="images/red-stripe-dark.jpg" /></li>
<li><img src="images/red-stripe.jpg" /></li>
<li><img src="images/red-stripe-red.jpg" /></li>
<li><img src="images/red-stripe-dark.jpg" /></li>
</ul>
</div>
<a class="buttons next" href="#"></a>
</div>
<div id="notice">
Flip through the images using the <strong> buttons </strong> and your mouse.
</div>
</div>
関連する CSS は次のとおりです。
#slider-code {
height: 125px;
overflow:hidden;
margin: 0 0 0 0;
}
#slider-code .viewport {
/* margin-left: auto; -- With this enabled, the arrows don't work.
margin-right: auto; */
float: left;
width: 240px;
height: 125px;
overflow: hidden;
position: relative;
}
#slider-code .viewport .overview img {
border: 4px solid #f6f6f7;
-moz-border-radius: 4px;
-khtml-border-radius: 4px;
-webkit-border-radius: 4px;
}
#slider-code .buttons {
display: block;
margin: 0 0 0 0; /* 10px 10px 0 0; */
float: left;
vertical-align: middle;
}
#slider-code .prev {
width: 32px;
height: 32px;
background: transparent url('../images/left-arrow.png') no-repeat 0 0;
vertical-align: middle;
margin: 0 0 0 0; /* top, right, bottom, left */
position: relative;
/* top: 190.5px; */
}
#slider-code .next {
width: 32px;
height: 32px;
background: transparent url('../images/right-arrow.png') no-repeat 0 0;
margin: 0 0 0 0px; /* 30px 0 0 10px; */
vertical-align: middle;
position: relative;
}
#slider-code .disable {
/* for IE */
filter:alpha(opacity=40);
/* for everything else */
opacity:0.4;
}
#slider-code .overview {
list-style: none;
position: absolute;
padding: 0;
margin: 0;
left: 0;
top: 0;
}
#slider-code .overview li {
float: left;
margin: 0 20px 0 0;
padding: 1px;
height: 121px;
border: 1px solid #dcdcdc;
width: 236px;
}
.view_name {
font-family: "Helvetica", serif;
color: #f9f4c0;
font-style: normal;
font-weight: bold;
font-size: 11px;
word-spacing: 0px;
letter-spacing: 0px;
background: #1a1a1a;
padding: 1px 3px 1px 3px; /* top, right, bottom, left */
-moz-border-radius: 5px;
-khtml-border-radius: 5px;
-webkit-border-radius: 5px;
}
#compare_view .compv-navbar img {
margin: 3px 3px -4px 3px;
}
#compare_view .compv-navbar img a.hover {
margin: 3px 3px -4px 3px;
}
ここにJSがあります:
$(window).load(function() {
// --------------------------- Begin Comparison Code --------------------------------------------------------
var win_width = $(window).width();
var num_of_images = 2; //The number of images expected in view (2 for 2-up, 3 for 3-up, etc.) The default value is 2.
$("#viewbar img").click(function(e) {
num_of_images = parseInt($(this).attr("data-id"), 10); // This accepts the integer associated with the navbar.
$('#viewname').text(num_of_images + '-up');
//--- All of this function is a duplicate of the 'default' case which is below. This is not very DRY-like, but it works for now.
var oImg_height = $('#slider-code .viewport .overview img:eq(1)').height(); //size of original image height
var oImg_width = $('#slider-code .viewport .overview img:eq(1)').width(); //size of original image width
var oImg_ratio = oImg_height / oImg_width; //aspect ratio of original image
var tImg_width = (win_width * 0.915) / num_of_images; // Target image width = (90% of the window) / 2
var tImg_height = tImg_width * oImg_ratio; // Target image height, resized according to the original image ratio.
var sliderCode_w = $('#slider-code').width();
var sliderCode_h = $('#slider-code').height();
var ul_width = $('#slider-code .viewport ul').width();
$('#slider-code .viewport .overview img:lt(26)').css({'width' : tImg_width, 'height' : tImg_height}); //resizes the images
var rImg_width = $('#slider-code .viewport .overview img:eq(1)').width(); // size of resized image width
var rImg_height = $('#slider-code .viewport .overview img:eq(1)').height(); // size of resized image width
$('#slider-code .next').css({'top' : rImg_height / 2}); //This needs to be resolved for various size windows
$('#slider-code .prev').css({'top' : rImg_height / 2});
$('#slider-code').css({'width': '100%', 'height': rImg_height + 10}); //to accomodate borders, extra padding was added to heights. To make it truly dynamic, a variable (as a percentage) of the width of the window, could be used to be added to the height
$('#slider-code .viewport').css({'width': win_width * 0.94, 'height': rImg_height + 10});
$('#slider-code .overview li').css({'width': rImg_width + 5});
var view_new_w = $('#slider-code .viewport').width();
var view_new_h = $('#slider-code .viewport').height();
var li_w = $('#slider-code .overview li').width();
var rUl_width = $('#slider-code .viewport ul').width();
$('#slider-code').tinycarousel({ controls: true, animation: true, display: 1 });
e.preventDefault();
});
//This is the default case that executes before a click is done. Because the code has been repeated above, it isn't very DRY-like.
var oImg_height = $('#slider-code .viewport .overview img:eq(1)').height(); //size of original image height
var oImg_width = $('#slider-code .viewport .overview img:eq(1)').width(); //size of original image width
var oImg_ratio = oImg_height / oImg_width; //aspect ratio of original image
var tImg_width = (win_width * 0.915) / num_of_images; // Target image width = (90% of the window) / 2
var tImg_height = tImg_width * oImg_ratio; // Target image height, resized according to the original image ratio.
var sliderCode_w = $('#slider-code').width();
var sliderCode_h = $('#slider-code').height();
var ul_width = $('#slider-code .viewport ul').width();
// console.log("Original Image Height: ", oImg_height, " Original Image Width: ", oImg_width, " Original Image Aspect Ratio: ", oImg_ratio, " Slider Code Width: ", sliderCode_w, " Slider Code Height: ", sliderCode_h, " Window Width: ", win_width, " UL Width: ", ul_width, " Target Image Width: ", tImg_width, " Target Image Height: ", tImg_height);
$('#slider-code .viewport .overview img:lt(26)').css({'width' : tImg_width, 'height' : tImg_height}); //resizes the images
var rImg_width = $('#slider-code .viewport .overview img:eq(1)').width(); // size of resized image width
var rImg_height = $('#slider-code .viewport .overview img:eq(1)').height(); // size of resized image width
$('#slider-code .next').css({'top' : rImg_height / 2}); //This needs to be resolved for various size windows
$('#slider-code .prev').css({'top' : rImg_height / 2});
$('#slider-code').css({'width': '100%', 'height': rImg_height + 10}); //to accomodate borders, extra padding was added to heights. To make it truly dynamic, a variable (as a percentage) of the width of the window, could be used to be added to the height
$('#slider-code .viewport').css({'width': win_width * 0.94, 'height': rImg_height + 10});
$('#slider-code .overview li').css({'width': rImg_width + 5});
var view_new_w = $('#slider-code .viewport').width();
var view_new_h = $('#slider-code .viewport').height();
var li_w = $('#slider-code .overview li').width();
var rUl_width = $('#slider-code .viewport ul').width();
// console.log("Viewport New Width: ", view_new_w, view_new_h, " List Item Width: ", li_w, " Resized Image Width: ", rImg_width, " Resized Image Height: ", rImg_height, " Resized UL Width: ", rUl_width);
$('#slider-code').tinycarousel({ controls: true, animation: true, display: 1 });
// --------------- End Comparison Code --------------------------------------------------------------------------
})
ところで、JS を window.load 関数に入れました。これは、コードが画像のサイズを変更する前にウィンドウ サイズを検出する必要があったためです。失速し、カルーセルに正しい画像をロードしません。
大変な作業だとは思いますが、コミュニティが私を助けてくれることを心から願っています。
ありがとう。
編集:刺すほど勇敢な人はいませんか?
編集 2: JonP によって提案された変更を行いましたが、解決しようとしている新しい問題がいくつかあります。通常の状態に戻す方法について何か提案はありますか? 上記のリンクをリロードして、新しいバージョンを表示します。