The jQuery plug-in boxScroll implements image carousel effects, and jqueryboxscroll

Source: Internet
Author: User

The jQuery plug-in boxScroll implements image carousel effects, and jqueryboxscroll

BoxScroll

Simple implementation of carousel effects for common images. You can control the number list or press the left or right buttons. The logic is very simple. At the end of the process, you have to go back and see the annotations.

The Code is as follows:

HTML

<! Doctype html> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> <meta content =" "name =" keywords "/> <meta content =" "name =" description "/> <meta name =" author "content = "codetker"/> 

CSS

@ Charset "UTF-8";/* CSS Document */body {margin: 0 0; padding: 0 0; height: 100%; width: 100% ;}. wrap {font-family: "", "", Times, "Times New Roman", serif; font-size: 14px; margin: 0 0; padding: 0 0; height: 100%; width: 100%; overflow: hidden ;}. boxStyle {/* photo size */width: 500px; height: 256px ;}. scrollBox {position: relative; width: 500px; margin: 0 auto ;}. picInnerBox {width: pixel PX;/* You can just put it down. If you need to scroll up or down, change height */overflow: hidden ;}. picInnerBox li {cursor: pointer; float: left ;}. picOuterBox {overflow: hidden ;}. arrow {position: absolute; top: 45%; height: 40px; cursor: pointer; z-index: 99 ;}. arrow: hover {color: # fff ;}. arrowLeft {float: left; left: 5% ;}. arrowRight {float: right; right: 5% ;}. picControl {overflow: auto; width: 100px; margin: 0 auto ;}. picControl ul li {cursor: pointer; float: left; width: 20px; height: 20px; text-align: center ;}. picControl ul li: hover {color: red ;}. liSelected {color: red ;}

JavaScript

/** BoxScroll 0.1 * compatible with other common browsers */; (function ($, window, document, undefined) {// defines the constructor var BoxObj = function (ele, opt) {this. $ element = ele; // The outermost layer object this. ults = {'style': 0, // select a scroll style. The default effect is 'speed': 1, // The default value is 1 s 'direction': 'left ', // The default value is 'left': $ (ele ). children ('. picouterbox '). children ('. arrowLeft '), // The important position 'toright' in the default format: $ (ele ). children ('. picouterbox '). children ('. arrowRight '), 'controlul': $ (ele ). chi Ldren ('. piccontrol '). children ('ul ')}, this. options = $. extend ({}, this. defaults, opt); // here you can add some common methods} // Add the method BoxObj to the constructor. prototype = {commonScroll: function () {// receiving Object Property var boxWindow = $ (this. $ element ). children ('. picouterbox '). children ('. picinnerbox'); var speed = this. defaults. speed; var style = this. defaults. style; var direction = (this. defaults. direction = 'left ')? 1:-1; var toLeft = this. defaults. toLeft; var toRight = this. defaults. toRight; var Control = this. defaults. controlUl; var boxWidth = $ (boxWindow ). children ('lil '). width (); var imgIndexMax =$ (boxWindow ). children ('lil '). length; var imgIndex; function getImgIndex () {// determine the position of the current image imgindex1_math.round(parseint(((boxwindow).css ("margin-left") * (-1)/boxWidth);} var timer; // It must be defined outside to ensure that only the timer = setInterval (Function () {boxScroll (imgIndex, direction) ;}, 5000); function rest () {clearInterval (timer); timer = setInterval (function () {boxScroll (imgIndex, direction) ;}, 5000) ;}// click the binding button $ (Control ). delegate ('lil', 'click', function () {boxScroll ($ (this ). index (), 0); rest () ;}); // bind the left and right buttons $ (toLeft ). click (function () {boxScroll (0,-1); rest () ;}); $ (toRight ). click (function () {boxScroll (0, 1); rest () ;}); function boxS Croll (index, dir) {if (! $ (BoxWindow). is (': animate') {// if (! Dir) {// responds to the ul li control operation // at this time, dir = 0, depending on the input imgIndex = index; // other times dir! = 0, then rely on dir} else {// response toLeft and toRight if (dir = 1) {// change getImgIndex () to the right; if (imgIndex = (imgIndexMax-1 )) {imgIndex = 0 ;}else {imgIndex + = 1 ;}} else {// left getImgIndex (); if (imgIndex = 0) {imgIndex = (imgIndexMax-1) ;}else {imgIndex-= 1 ;}}$ (Control ). children ('lil '). eq (imgIndex ). addClass ('liselected'); $ (Control ). children ('lil '). eq (imgIndex ). siblings (). removeClass ('liselected'); $ (boxWindow ). animate ({"margin-left": imgIndex * boxWidth * (-1) + 'px '}, 1000 * speed );}}}} // The method for using the windowObj object in the plug-in. 0 is vertical, and 1 is horizontal $. fn. boxScroll = function (options) {// create an object var boxObj = new BoxObj (this, options); // call the object method return boxObj in the form of a tail call. commonScroll () ;}}) (jQuery, window, document );

For more information, see https://github.com/codetker/myboxscroll.

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.