Home  >  Article  >  Web Front-end  >  Let's talk about some of the effects that jquery+css can achieve

Let's talk about some of the effects that jquery+css can achieve

PHPz
PHPzOriginal
2023-04-25 10:47:33609browse

With the continuous development of Internet technology, the design of web pages has become more and more colorful. Using jQuery and CSS can achieve a variety of special effects to make web pages more beautiful and interesting. This article will introduce some implementation effects of using jQuery and CSS in web design.

1. Image carousel effect

In web pages, carousel images are a common special effect and can be used to display products, advertisements, etc. Many different carousel effects can be created through jQuery and CSS, such as left and right sliding, fade in and fade out, zoom and other effects. The following is a sample code to achieve the left and right sliding image carousel effect:

HTML code:

<div class="slider">
    <ul>
        <li><img src="img/1.jpg"></li>
        <li><img src="img/2.jpg"></li>
        <li><img src="img/3.jpg"></li>
    </ul>
</div>

CSS code:

.slider {
    position: relative;
    overflow: hidden;
}
.slider ul {
    list-style: none;
    position: absolute;
    width: 300%;
    height: 100%;
    margin: 0;
    padding: 0;
}
.slider li {
    float: left;
    width: 33.3333%;
    height: 100%;
    display: block;
}
.slider img {
    width: 100%;
    height: 100%;
    display: block;
}

JavaScript code:

$(document).ready(function() {
    var currentIndex = 0,
        items = $('.slider li'),
        itemAmount = items.length;
  
    function cycleItems() {
        var item = $('.slider li').eq(currentIndex);
        items.hide();
        item.css('display','inline-block');
    }
  
    var autoSlide = setInterval(function() {
        currentIndex += 1;
        if (currentIndex > itemAmount - 1) {
            currentIndex = 0;
        }
        cycleItems();
    }, 3000);
});

2. Drop-down menu effect

The drop-down menu is one of the elements often used in web design. It can realize multi-level menus and allow users to understand the navigation structure of the website more clearly. Using jQuery and CSS, you can achieve various drop-down menu effects, such as mouse hover, click, slide and other interactive forms. Here is a sample code for a drop-down menu that implements a mouseover effect:

HTML code:

<ul class="nav">
    <li><a href="#">Home</a></li>
    <li>
        <a href="#">About</a>
        <ul>
            <li><a href="#">Our Company</a></li>
            <li><a href="#">Our Team</a></li>
            <li><a href="#">Testimonials</a></li>
        </ul>
    </li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>
</ul>

CSS code:

.nav {
    list-style: none;
    margin: 0;
    padding: 0;
}
.nav li {
    position: relative;
    float: left;
    width: 150px;
    text-align: center;
    margin-right: 5px;
}
.nav a {
    display: block;
    padding: 5px 10px;
    color: #333;
    background-color: #f2f2f2;
}
.nav a:hover {
    background-color: #333;
    color: #fff;
}

/* 下拉菜单 */
.nav ul {
    position: absolute;
    top: 30px;
    left: 0;
    width: 150px;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}
.nav ul li {
    width: 100%;
    text-align: left;
    margin: 0;
    padding: 0;
}
.nav ul a {
    padding: 5px 10px;
    background-color: #ccc;
    color: #333;
    display: block;
}
.nav ul a:hover {
    background-color: #333;
    color: #fff;
}

JavaScript code:

$(document).ready(function() {
    $('.nav li').hover(
        function () {
            $('ul', this).slideDown(100);
        },
        function () {
            $('ul', this).slideUp(100);
        }
    );
});

三、Scrolling effect

The scrolling effect can make the web page more dynamic and interesting. Various scrolling effects can be achieved using jQuery and CSS, such as smooth scrolling, scrolling to a specified position, etc. The following is a sample code to achieve a smooth scrolling effect:

HTML code:

<ul class="nav">
    <li><a href="#section-1">Section 1</a></li>
    <li><a href="#section-2">Section 2</a></li>
    <li><a href="#section-3">Section 3</a></li>
    <li><a href="#section-4">Section 4</a></li>
</ul>

<div id="section-1" class="section">
    <h2>Section 1</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vestibulum tellus in lorem tristique porttitor. Sed euismod, metus vel elementum commodo, leo ante suscipit lorem, in aliquet sapien augue vitae odio.</p>
</div>
<div id="section-2" class="section">
    <h2>Section 2</h2>
    <p>Sed euismod, metus vel elementum commodo, leo ante suscipit lorem, in aliquet sapien augue vitae odio. Nullam vestibulum tellus in lorem tristique porttitor.</p>
</div>
<div id="section-3" class="section">
    <h2>Section 3</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vestibulum tellus in lorem tristique porttitor. Sed euismod, metus vel elementum commodo, leo ante suscipit lorem, in aliquet sapien augue vitae odio.</p>
</div>
<div id="section-4" class="section">
    <h2>Section 4</h2>
    <p>Sed euismod, metus vel elementum commodo, leo ante suscipit lorem, in aliquet sapien augue vitae odio. Nullam vestibulum tellus in lorem tristique porttitor.</p>
</div>

CSS code:

.section {
    margin: 100px 0;
    padding: 50px;
    background-color: #f2f2f2;
}

JavaScript code:

$(document).ready(function() {
    $('a').click(function(){
        $('html, body').animate({
            scrollTop: $( $(this).attr('href') ).offset().top
        }, 500);
        return false;
    });
});

The above are three There are sample codes for effects achieved using jQuery and CSS, namely image carousel, drop-down menu and scrolling effect. With the development of technology, there are many other effects that can be achieved. As long as you add creativity and imagination, you can create cooler web design effects.

The above is the detailed content of Let's talk about some of the effects that jquery+css can achieve. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to view CSS stylesNext article:How to view CSS styles