我正在按照本教程构建移动滑块:http: //mobile.smashingmagazine.com/2012/06/21/play-with-hardware-accelerated-css/
唯一的区别是,我正在尝试将其合并到 Backbone 中。但是,我遇到了一个变量问题。任何人都可以就如何使其发挥作用提供一些建议。在 View 的函数中传递这些变量是问题所在:
var sliding = startClientX = startPixelOffset = pixelOffset = currentSlide = 0,
slideCount = $('.slide').length;
全骨干视图:
WhiteDeals.Views.EditorView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render', 'slideStart', 'slide');
var sliding = startClientX = startPixelOffset = pixelOffset = currentSlide = 0,
slideCount = $('#dealSlide').length;
},
events: {
"touchstart" : "slideStart",
"mousedown" : "slideStart",
"mouseup" : "slideEnd",
"touchend" : "slideEnd",
"mousemove" : "slide",
"touchmove" : "slide"
},
slideStart: function(event) {
if (event.originalEvent.touches)
event = event.originalEvent.touches[0];
if (sliding == 0) {
sliding = 1;
startClientX = event.clientX;
}
}, // End slideStart
slide: function(event) {
event.preventDefault();
if (event.originalEvent.touches)
event = event.originalEvent.touches[0];
var deltaSlide = event.clientX - startClientX;
if (sliding == 1 && deltaSlide != 0) {
sliding = 2;
startPixelOffset = pixelOffset;
}
if (sliding == 2) {
var touchPixelRatio = 1;
if ((currentSlide == 0 && event.clientX > startClientX) ||
(currentSlide == slideCount - 1 && event.clientX < startClientX))
touchPixelRatio = 3;
pixelOffset = startPixelOffset + deltaSlide / touchPixelRatio;
$('#dealSlider').css('transform', 'translate3d(' + pixelOffset + 'px,0,0)').removeClass();
}
},
slideEnd: function(event) {
if (sliding == 2) {
sliding = 0;
currentSlide = pixelOffset < startPixelOffset ? currentSlide + 1 : currentSlide - 1;
currentSlide = Math.min(Math.max(currentSlide, 0), slideCount - 1);
pixelOffset = currentSlide * -$('body').width();
$('#temp').remove();
$('<style id="temp">#dealSlider.animate{transform:translate3d(' + pixelOffset + 'px,0,0)}</style>').appendTo('head');
$('#dealSlider').addClass('animate').css('transform', '');
}
}, // End slideEnd
render: function() {
this.$el.html(JST['editor/view']());
return this;
}
}); // End of Views EditorView