帮助修改我的自定义JQuery滑块,以允许连续滚动
Help with modifying my custom JQuery slider to allow continous scrolling
你可以在这里查看我的自定义滑块:http://www.awaismuzaffar.com/examples/index.html
下面是它的JQuery代码: $(document).ready(function() {
// Slider Function
var slideWidth = $('div.slide').width();
// Set the left position of each div element
$('div.slide').each(function(index){
$(this).css('left', index * slideWidth ); // Multiply each div element by the index(0, 1 etc) so each div is placed inline
});
// Next step is to animate the div elements
var clickCount = 1;
var slideCount = $('div.slide').length;
// Set the previous button to hide when loading with the first slide
if(clickCount == 1){
$('a#previous-button').css('background-color', '#cccccc');
}
$('a#next-button').click(function() {
if(clickCount < slideCount) {
$('div.slide').animate({"left":"-=" + slideWidth}, 'slow');
$('a#previous-button').css('background-color', '#ffffff');
clickCount++;
}
if(clickCount == slideCount) {
$('a#next-button').css('background-color', '#cccccc'); // Hide or grey out button
}
});
$('a#previous-button').click(function() {
if(clickCount > 1){
$('div.slide').animate({"left":"+=" + slideWidth}, 'slow');
$('a#next-button').css('background-color', '#ffffff');
clickCount--;
}
if(clickCount == 1){
$('a#previous-button').css('background-color', '#cccccc'); // Hide or grey out button
}
});
});
我正在尝试修改这个滑块以允许连续滚动。
我不确定如何实现这一点,我假设我需要使用append,但我不确定如何使用它。
谢谢。
你做的事情有点手动,我想别人可能已经解决了这个问题。但无论如何,在点击下一步按钮时,您需要在点击结束时加载额外的内容。如果我是你,我会这样做:
$('a#next-button').click(function() {
...
if(clickCount == slideCount) {
$('a#next-button').css('background-color', '#cccccc');
$.get(moreContentUrl, objectRepresentingCurrentScrollPosition, loadContent);
spinner.show(); // show some kind of spinner here (you can also hook up a default spinner on all ajax events with a global ajax handler
}
});
function loadContent(response) {
// append your content (your controller should return just the <div class="slide" /> elements, and give them a class="slide newSlide" so you can distinguish them below
// you can also do client side templating here. would be more efficient, then just return the items as objects instead of html
$('#slide-container').append(response.itemsHtml);
// slide all new divs right
$('div.newSlide').animate({"left":"+=" + slideWidth}, 'fast');
$('div.newSlide').removeClass('newSlide');
// update variables and un-grey the next button
$('a#previous-button').css('background-color', '#ffffff');
slideCount += response.itemCount;
// hide the spinner shown when starting the load
spinner.hide();
}
试一试,希望能成功。现在,为了稍微清理一下代码,我建议使用css类代替内联背景颜色等
这是一个解决方案(有点像Shaz的),只是更少的代码:):
$(document).ready(function(){
$('#previous-button').click(function(){slidePanel(-1)});
$('#next-button').click(function(){slidePanel(1)});
var n = 0;
var animating = false;
$('#slide-'+n).css('display', 'block');
function slidePanel(delta)
{
if(!animating)
{
animating = true;
var d = (delta > 0 ? $('#slide-'+n).width()*-1 : $('#slide-'+n).width());
$('#slide-'+n).animate({
left: "+="+d
}, 'slow', function() { $(this).css('display', 'none'); animating = false; });
n = (n + delta) % $('div.slide').length;
n = n < 0 ? $('div.slide').length + n : n;
$('#slide-'+n).css('left', $('#slide-container').offset().left +(d*-1));
$('#slide-'+n).css('display', 'block');
$('#slide-'+n).animate({
left: 0
}, 'slow');
}
}
});
点击这里查看示例。
(我知道有一种方法可以用一个线性方程计算出"当前幻灯片",但我想我现在已经脑死亡了:p)
(为避免重复点击而编辑)
试试这样做:http://fiddle.jshell.net/Shaz/dsBkf/
相关文章:
- 使用CI和CodeDeploy对node.js应用程序进行连续部署
- 连续两页返回Javascript
- 匹配 2D 数组中的连续值
- 如何做javascript连续滚动-自动加载
- firebase中有没有一种方法可以防止快速连续写入
- 我如何让我的脚本连续提交两个表单
- 如何限制同一个字符连续使用
- 订阅一个具有角度的websocket的连续数据
- Regex或javascript每X个连续单词中有一个单词
- 如何检测每个连续字符与前一个字符相差最小字母差的字符序列(1)
- 砖石网格连续循环卷轴
- KeyDown不会'按住时不能连续开火
- 在连续第二次单击时禁用href
- Chrome:如何在不嵌套的情况下将两个连续跨距插入可编辑实体中
- 创建一个javascript数组(N),该数组由连续的较小整数组成,其和为1000
- Javascript连续函数
- 如何通过箭头键连续/平滑地移动元素
- 比较数组中的连续元素不会返回任何结果(javascript)
- 正则表达式允许字母数字和空格,但不允许连续两个空格
- 如何使用 javascript 查找字符串中是否包含特定数量的连续连续数字