Định nghĩa và sử dụng .fadeTo() trong jQuery

.fadeTo(): Hiển thị các thành phần phù hợp với hiệu ứng mờ dần (fade).

Cấu trúc .fadeTo() trong jQuery

Đã được thêm vào từ phiên bản 1.0

.fadeTo(Độ bền,độ mờ)

Độ bền có thể bằng số hoặc bằng chữ: slow, fast.

Độ mờ (opacity): có giá trị từ 0 tới 1

$('p').fadeTo(300,0.3);
$('p').fadeTo(fast,0.6);
.fadeTo(Độ bền,độ mờ,function(){...})

$('p').fadeTo(300,0.4,function(){
    $('span').fadeIn(100);
});

Đã được thêm vào từ phiên bản 1.4.3

.fadeTo(Độ bền,độ mờ,'easing',function(){...})

$('p').fadeTo(300,0.4,'swing',function(){
    $('span').fadeIn(100);
});

.fadeTo(Độ bền)

Html viết:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tiêu đề</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style>
div {
    background-color: blue;
    float: left;
    margin-right: 20px;
    height: 100px;
    width: 150px;
}
p {
    clear: both;
}
</style>
<script>
$(function(){
    $('button').click(function(){
        $('.test03').fadeTo(2000,0.4);
        $('.test02').fadeTo('slow',0.4);
    });
});
</script>
</head>

<body>
<p><button>Click</button></p>
<div class="test03">fadeTo(2000,0.4)</div>
<div class="test02">fadeTo(slow,0.4)</div>
</body>
</html>

Hiển thị trình duyệt:

Click vào button để thấy hiệu ứng.

.fadeTo(Độ bền,function(){...})

Html viết:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tiêu đề</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style>
div {
    background-color: blue;
    float: left;
    margin-right: 20px;
    height: 100px;
    width: 150px;
}
p {
    clear: both;
}
</style>
<script>
$(function(){
    $('button').click(function(){
        $('div').fadeTo(2000,0.3,function(){
            $(this).css('background-color','red');
        });
    });
});
</script>
</head>

<body>
<p><button>Click</button></p>
<div>Thành phần div</div>
</body>
</html>

Hiển thị trình duyệt:

Click vào button để thấy hiệu ứng, sau khi hoàn thành hiệu ứng sẽ hiển thị nội dung bên trong function.

.fadeTo(Độ bền,độ mờ,'easing',function(){...})

Html viết:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tiêu đề</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style>
div {
    background-color: blue;
    float: left;
    margin-right: 20px;
    height: 100px;
    width: 150px;
}
p {
    clear: both;
}
.visible {
    background-color: red;
    display: block!important;
}
</style>
<script>
$(function(){
    $('button').click(function(){
        $('.swing').fadeTo(2000,0.3,'swing',function(){
            $(this).addClass('visible').text('Nội dung mới');
        });

        $('.linear').fadeTo(2000,0.3,'linear',function(){
            $(this).addClass('visible').text('Nội dung mới');
        });
    });
});
</script>
</head>

<body>
<p><button>Click</button></p>
<div class="swing">fadeTo swing</div>
<div class="linear">fadeTo linear</div>
</body>
</html>

Hiển thị trình duyệt:

Click vào button để thấy hiệu ứng, sau khi hoàn thành hiệu ứng sẽ hiển thị nội dung bên trong function.

Viết câu trả lời

Drop Images

0 Bình luận