jQuery implementation of widescreen image carousel tutorial_jquery
This article describes an example tutorial of implementing widescreen image carousel with jQuery. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:
Introduce jquery library
<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
Build html
The entire code is divided into three parts:
1. Loading part;
2. In the picture part, there can only be 4 pictures here. Friends who are interested can improve it;
3. The TAB button part. Of course, there are only 4 buttons here, and they also need to be improved.
<div class="gg" id="gg"> <div class="ggLoading"> <div class="ggLoading2"><em>精彩活动载入中</em></div> </div> <div class="ggs"> <div class="ggBox" id="ggBox"> <a href="#" title="5月22日测试开启领报名资格" style="z-index: 3; opacity: 4;"> <img src="images/1.jpg" alt="" /></a> <a href="#" title="首测世界的雕琢篇章开启"> <img src="images/2.jpg" alt="" /></a> <a href="#" title="上古世纪游戏资料手册"> <img src="images/3.jpg" alt="" /></a> <a href="#" title="游戏四大特色揭晓"> <img src="images/4.jpg" alt="" /></a> </div> </div> <div class="ggb"> <div class="ggBtns" id="ggBtns"> <a title="5月22日测试开启领报名资格" href='javascript:void(0)' class="ggOn"><em>5月22日测试开启领报名资格</em></a> <a title="首测世界的雕琢篇章开启" href='javascript:void(0)'><em>首测世界的雕琢篇章开启</em></a> <a title="上古世纪游戏资料手册" href='javascript:void(0)'><em>上古世纪游戏资料手册</em></a> <a title="游戏四大特色揭晓" href='javascript:void(0)'><em>游戏四大特色揭晓</em></a> </div> </div> </div>
CSS style
The CSS here can be customized according to project needs. You don’t have to stick to the code below, as long as you understand the principles. If you don’t understand the CSS below, just make up for it. I won’t explain it one by one here.
.ggLoading, .ggLoading2 { background-image: url(../images/nav.png); } .gg { width: 100%; height: 500px; position: relative; z-index: 1; overflow: hidden; margin: 0 auto; background: #d3d3d3 url(../images/loading.jpg) repeat-x; } .ggLoading { position: absolute; left: 40%; top: 200px; width: 325px; text-align: center; height: 56px; background-position: 0 -274px; background-repeat: no-repeat; line-height: 56px; color: #9c9c9c; } .ggLoading2 { width: 330px; height: 56px; background-position: 213px -330px; background-repeat: no-repeat; } .ggLoading em { font-weight: bold; } .ggs { width: 200%; height: 500px; left: -50%; top: 0; position: absolute; } .ggBox { width: 1920px; height: 500px; margin: 0 auto; } .ggBox a { display: block; width: 1920px; height: 500px; position: absolute; z-index: 1; opacity: 0.1; } .ggBox img { display: block; width: 1920px; height: 500px; } .ggb { position: absolute; width: 100%; left: 0; bottom: 0; height: 40px; z-index: 4; background-color: #32342e; background-repeat: repeat-x; background-position: 50% -40px; } .ggBtns { width: 960px; height: 40px; margin: 0 auto; border-left: 1px solid #090908; border-right: 1px solid #6a6a60; } .ggBtns a { float: left; display: block; width: 240px; height: 40px; text-align: center; padding-top: 10px; color: #848380; font-size: 14px; line-height: 40px; background-position: 0 10px; position: relative; top: -10px; outline: none; background-repeat: no-repeat; cursor: pointer; } .ggBtns a em { display: block; width: 210px; height: 40px; margin: 0 auto; overflow: hidden; } .ggBtns a:hover { color: #e7e7e7; } .ggBtns a:focus { outline: none; } .ggBtns a.ggOn { color: #e7e7e7; background-position: 0 0; } .ggb, .ggBtns a { background-image: url(../images/main.jpg); } a.ggOn { background-image: url(../images/gg.png); }
JS code
Finally, we have come to the important part. There is not much code in this part, so let’s take a look at it.
$(function () {//文档加载后执行 //定义$con,$box,$btns,$i变量,autoChange自动播放函数,loop定时器。 var $con = $('#gg'), $box = $con.find('#ggBox'), $btns = $con.find('#ggBtns'), i = 0, autoChange = function () { i += 1;//计数器+1 if (i === 4) { i = 0; }//如果计数器i等4就把i重置为0. $btns.find('a:eq(' + i + ')').addClass('ggOn').siblings().removeClass('ggOn'); //找到TAB按钮中的第i个a标签,为其加上ggOn的样式,同时移除所有同级的a标签ggOn样式 var curr = $box.find('a:eq(' + i + ')'), prev = curr.siblings(); //定义curr变量,并赋值为$box中当前显示图片的a标签,定义prev变量,赋值为$box中除了当前显示图片的A标签外的所有A标签。 prev.css('z-index', 2);//$box中除了当前显示图片的A标签外的所有A标签的index值变为2,即向下移一层 curr.css('z-index', 3).animate({ //$box中当前显示图片的a标签index值变为3,即向上移一层,然后使用jquery动画以150毫秒把透明度变为1,之后执行匿名函数function。 'opacity': 1 }, 150, function () { //$box中除了当前显示图片的A标签外的所有A标签的index值变为1,并把透明度变为0.1 prev.css({ 'z-index': 1, 'opacity': 0.1 }); }); }, loop = setInterval(autoChange, 5000);//定义定时器,每5秒执行一次autoChange函数,达到自动播放效果。 $con.hover(function () { //定义鼠标悬浮与离开事件 clearInterval(loop); //鼠标悬浮时移除Loog定时器,即停止播放 }, function () { loop = setInterval(autoChange, 5000); //鼠标离开时载放Loog定时器,继续播放 }); $btns.find('a').click(function () {//定义tab按钮事件 i = $(this).index() - 1; //tab按钮中当前A标签的index值-1,并赋值给i计数器 autoChange(); //调用切换方法切换图片 }); });
I wonder if you guys understand the principle after reading the above notes? In fact, the entire code is divided into four parts:
1. Image switching
Use i as the counter, display the picture currently for i, hide all other pictures, add the ggOn style to the button currently for i, remove the ggOn style for other buttons, and i will increment each time the switching function is called 1.
2. Automatic play
Define a timer loop to call the switching function every 5 seconds.
3. Mouse hover event
It turns out that the loop timer is cleared when the mouse is hovering, and the loop timer is loaded when the mouse leaves.
4. Button event
Bind the tab button click event. After clicking, assign i the index value of the current tab button -1, and call the switching function.
The above is all the key code for jquery to implement image carousel. I hope you will study it carefully. There are still many shortcomings in the tutorial and I hope you can improve it.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
