swiper,
SuperSlideSo I am not going to develop a complete one, here is a simple slide3D, easy to introduce the implementation principle of the rotation.
slide
Switching effect)Need the following
:
Copy the code
Flip, turn, flat, fragment
Layout:
Copy the code
new Slide3D(contianer, params)
Initialize oneSlide3D
, returns the initializedSlide3D
Instance.
Var mySlide = new Slide3D('. Flip ', {width: 300, height: 400, // define high direction: 'horizontal | vertical', / / switch direction, only slide support effect: 'flip', / / switching effect flip | turn | slide | flat | fragments sources: ['image-slider-1.jpg', 'image-slider-2.jpg', 'image-slider-3.jpg', 'image-slider-4.jpg', 'image-slider-5.jpg'], // Slide effects do not require rows: 6, // cols: 3 // columns}, pagination: True, // whether to add pager loop: true // whether to add pager loop.Copy the code
slide
Switch effect, I believe you are rightslide
Switching effect will be realized, the difficulty is to cycle, how to achieve it?
wrapper3D
This is the layer that slides.
if(s.params.loop) {
var firstChild = s.wrapper.firstElementChild.cloneNode(true);
var lastChild = s.wrapper.lastElementChild.cloneNode(true);
s.wrapper.appendChild(firstChild);
s.wrapper.insertBefore(lastChild, s.wrapper.firstElementChild);
s.setTransitionDuration(s.wrapper, 0);
s.currentIndex = 1;
s.slideTo(s.activeIndex + 1);
};
Copy the code
loop
Properties fortrue
When, we will be the firstslide3D
And the last oneslide3D
Copy the contents of, and insert them as the last and first child, respectivelywrapper3D
Element.
/* omit the original 5 slide3D */
/* Omit the pager and the forward and backward buttons */
Copy the code
wrapper3D
Element slide oneslide3D
The width of the element, otherwise you will see the last image.
s.slideTo(s.activeIndex + 1); s.slideTo = function(index) { if(s.params.direction == 'horizontal') { s.setTransform(s.wrapper, 'translate3d(-' + (s.params.width * index) + 'px, 0, 0)'); }};Copy the code
wrapper3D
Elements of thetransform
It goes like this:
transform: translate3d(-400px, 0, 0)Copy the code
transform: translate3d(-400px, 0, 0) => transform: translate3d(0, 0, 0)Copy the code
transition-duration
Set it to 0, then:
transform: translate3d(-400 * 5px , 0, 0)Copy the code
Transition-duration = 0, so the transition is instantaneous, which tricks our eyes, so we can’t see it.
This is how the slide toggle loop works, of course, there are more ways you can try it, but the other toggle loops (flip, turn, Flat, fragment) are pretty much the same.
The first thing to do is to classify the image:
var r = s.params.box.rows; var c = s.params.box.cols; var width = Math.ceil(s.params.width / c); Var height = math.ceil (s.params.height/r); Var length = total = r * c; Var cssText = ''; for(var i = 0; i < length; i++) { var left = i % c * width; var top = Math.floor(i / c) * height; var flipItem = document.createElement('div'); if(s.params.effect == 'fragment') { cssText += 'opacity:0; '; }; cssText += 'position:absolute; left:' + left + 'px; top:' + top + 'px; '; cssText += 'width:' + width + 'px; height:' + height + 'px; '; cssText += 'background-image:url(' + url + '); '; cssText += 'background-position:-' + left + 'px -' + top + 'px; '; cssText += 'background-size:' + s.params.width + 'px ' + s.params.height + 'px; '; cssText += 'background-repeat:no-repeat; '; flipItem.style.cssText = cssText; flipItem.change = false; animation[s.params.effect].execute(flipItem); // Remember this function, s.whopper. appendChild(flipItem); / / insert};Copy the code
It is estimated that you will see the above code will be more chaotic, the principle is similar to the table, equivalent to a TD corresponding to a div, their own code to achieve partition try to know.
Let’s analyze the fragment switching effect:
Define an animation function:
animate: function(item, fn, fnEnd, index) { var opacity = 0; var data = []; clearInterval(item.timer); Item.timer = setInterval(function() {// Use a timer for animating += 0.05; Data ['opacity'] = opacity; data['index'] = index; if(fn) fn.call(item, data); Opacity >= 1 {clearInterval(item.timer); if(fnEnd) fnEnd.call(item); }}}, 20);}}};}}}; }Copy the code
Animate each item (that is, each cell) and pass the parameter.
>animation[s.params.effect].execute(flipItem);Copy the code
The following code is executed:
(function(item) { setTimeout(function() { animation[s.params.effect].animate(item, function(data) { item.style.opacity = data['opacity']; }, function() {if(--total == 0) {if(--total == 0) {// When all items are animated, execute the code in the if block. s.lock = false; end(); s.wrapper.style.backgroundImage = 'url(' + s.params.sources[s.activeIndex] + ')'; } }, i); }, i * 50); })(item);})(item);})(item); // Execute the function immediatelyCopy the code
Other switching effects work the same way.
Once you understand the principles, you can develop even cooler effects.
If you have any questions, feel free to ask them below!