的一種可能的解決方案(多種解決方案存在):
DEMO
的Html
<a href="#" class="prev_p">Previous</a>
<a href="#" class="start">Start</a>
<a href="#" class="next_p">Next</a>
<div id="projects">
<div class="slide gray"></div>
<div class="slide white"></div>
<div class="slide black"></div>
<div class="slide gray"></div>
<div class="slide white"></div>
<div class="slide black"></div>
</div>
CSS
#projects {
position:relative;
float:left;
width:9800px;
height:300px;
overflow:hidden;
}
.slide {
position:relative;
float:left;
border:1px solid black;
width:1960px;
height:300px;
}
.gray {
background-color:gray;
}
.white {
background-color:white;
}
.black {
background-color:black;
}
jQuery的
$(function() {
$(".prev_p").hide();
$(".start").hide();
var slidewidth = 1960; //in Pixels
var animationSpeed = 300; //in Milliseconds
//start page
$(".start").click(function() {
$("#projects").animate({
marginLeft: "0px"
}, animationSpeed, function() {
$(".start").hide();
$(".prev_p").hide();
$(".next_p").show();
});
});
//next page
$(".next_p").click(function() {
$("#projects").animate({
marginLeft: "-=" + slidewidth + "px"
}, animationSpeed, function() {
//check location after animation and hide controls
//that no longer serve a purpose and add those that do
var marginLeft = $("#projects").css("margin-left");
var numberOfSlides = 5;
if (marginLeft == "-" + (slidewidth * (numberOfSlides - 2)) + "px") {
$(".next_p").hide();
}
if (marginLeft != "0px") $(".start").show();
else $(".start").hide();
$(".prev_p").show();
});
});
//previous page
$(".prev_p").click(function() {
$("#projects").animate({
marginLeft: "+=" + slidewidth + "px"
}, animationSpeed, function() {
//check location after animation and hide controls
//that no longer serve a purpose and add those that do
var marginLeft = $("#projects").css("margin-left");
if (marginLeft == "0px") {
$(".prev_p").hide();
$(".start").hide();
} else {
$(".start").show();
}
$(".next_p").show();
});
});
});
你是一個真正的紳士和學者,先生。這是拯救生命和完美..我想要的東西只是簡單的,沒有插件,這就做到了。這是我用http://jsfiddle.net/GPXsn/ – Sneezy 2013-03-07 00:39:02
結束嘿邁克爾..所以我不得不回去重做我的結構,使一切響應和擺脫固定的寬度。現在我一直在努力爭取讓這個jQuery得到修復以適應新的結構..任何提示?目前它的圖像看起來像全寬點擊。我設置了一個容器寬度,它似乎可以在更大的分辨率下工作,但在更小的時候仍然會變大。這是沒有意義的。再次感謝! – Sneezy 2013-03-08 22:34:46
@Sneezy更新了jsFiddle以在樣式表中使用幻燈片的寬度。因此,您應該能夠將其設置爲與移動設備不同的東西,並且代碼的工作原理也是一樣的。 http://jsfiddle.net/KML9k/2/如果您調整瀏覽器的大小,這將無法正常工作,因爲它是在頁面加載時設置的。您可以使用onResize函數根據需要更改大小。但是這應該適合你的目的。 – 2013-03-08 23:00:36