本文实例为大家介绍了基于jQuery实现淡入淡出轮播效果的关键代码,分享给大家供大家参考,具体内容如下:
基本原理:将所有图片绝对定位在同一位置,透明度设为0,然后通过jQuery的淡入淡出实现图片的切换效果。
html代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>一个轮播</title>
<style>
#scrollPlay{
width: 730px;
height: 336px;
/*overflow: hidden;*/
}
#pre{
position: absolute;
margin-top: 150px;
width:30px;
height: 30px;
background: #000;
color:#fff;
opacity: 0.7;
text-align: center;
line-height: 30px;
font-size: 20px;
z-index: 10;
}
img{
opacity: 0;
position: absolute;
}
#next{
position: absolute;
margin-left:700px;
margin-top: 150px;
width:30px;
height: 30px;
background: #000;
color:#fff;
opacity: 0.7;
text-align: center;
line-height: 30px;
font-size: 20px;
z-index: 10;
}
span{
display: block;
width: 15px;
height: 15px;
float: left;
border: 1px solid #fff;
}
#buttons{
position: absolute;
background: #000;
margin-top: 300px;
margin-left: 300px;
z-index: 10;
}
.onactive{
background: green;
}
</style>
<script src='jquery.js'></script>
<script src='index.js'></script>
</head>
<body>
<div id='scrollPlay'>
<div id='buttons'>
<span index = 0 class='onactive'></span>
<span index = 1></span>
<span index = 2></span>
<span index = 3></span>
<span index = 4></span>
</div>
<div id='pre'><</div>
<div id='next'>></div>
<img src='images/1.jpg' alt='pics' style='opacity:1'>
<img src='images/2.jpg' alt='pics'>
<img src='images/3.jpg' alt='pics'>
<img src='images/4.jpg' alt='pics'>
<img src='images/5.jpg' alt='pics'>
</div>
</body>
</html>
JS:
$(function(){
var index = 0;
var flag = false; //用于判断是否处于动画状态
//切换函数
function move(offset){
flag=true;
//console.log(offset)
$('img').eq(index).fadeOut('slow',function(){
if(offset>0){
if(index ==4){
index=0;
}else{
//console.log(index);
index=index+offset;
//console.log(index);
}
}
if(offset<0){
if(index==0){
index=4;
}else{
index=index+offset
}
}
$('img').eq(index).fadeTo('slow',1) //使用fadeIn不成功:$('img').eq(index).fadeIn('slow')
showButton();
flag=false;
});
}
//点击切换上一张
$('#pre').click(function(){
if(flag==false){
move(-1)
}
})
//点击切换下一张
$('#next').click(function(){
if(flag==false){
move(1)
}
})
//点击按钮直接切换
$('span').click(function(){
if(flag==false){
var i= $(this).attr('index')
//console.log(i)
//console.log(index)
//console.log(i-index)
move(i-index)
showButton();
}
})
//高亮显示按钮
function showButton(){
if($('span').hasClass('onactive')){
$('span').removeClass();
}
$('span').eq(index).addClass('onactive')
}
//自动播放
var timer;
function go(){
timer = setInterval(function(){
$('#next').trigger('click');
},3000)
}
//鼠标悬停,清除自动播放
$('#scrollPlay').mouseover(function(){
clearInterval(timer)
})
//鼠标移开,开始自动播放
$('#scrollPlay').mouseout(function(){
go();
})
go();
})
文章最后为大家提了一个小问题,希望大家能给出解决方法。
使用fadeIn淡入时却无效果,最后只能使用fadeTo实现,这是什么原因?
为大家分享一个小例子:原生JS实现淡入淡出效果(fadeIn/fadeOut/fadeTo)
淡入淡出效果,在日常项目中经常用到,可惜原生JS没有类似的方法,而有时小的页面并不值得引入一个jQuery库,所以就自己写了一个,已封装, 有用得着的朋友, 可以直接使用. 代码中另附有一个设置元素透明度的方法, 是按IE规则(0~100)设置, 若改成标准设置方法(0.00~1.00), 下面使用时请考虑浮点精确表达差值.
参数说明:
fadeIn()与fadeOut()均有三个参数,第一个是事件, 必填; 第二个是淡入淡出速度, 正整数, 大小自己权衡, 可选参数; 第三个, 是指定淡入淡出到的透明度值(类似于jQuery中的fadeTo()), 0~100的正整数值, 也是可选参数.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>原生JS实现淡入淡出效果</title>
<style>
/*demo css*/
#demo div.box {float:left;width:31%;margin:0 1%}
#demo div.box h2{margin-bottom:10px}
#demo div.box h2 input{padding:5px 8px;font-size:14px;font-weight:bolder}
#demo div.box div{text-indent:10px; line-height:22px;border:2px solid #555;padding:0.5em;overflow:hidden}
</style>
<script>
window.onload = function(){
//底层共用
var iBase = {
Id: function(name){
return document.getElementById(name);
},
//设置元素透明度,透明度值按IE规则计,即0~100
SetOpacity: function(ev, v){
ev.filters "demo">
<div class="box">
<h2><input type="button" value="点击淡入" /></h2>
<div id="fadeIn" style="display:none">
<p></p>
<p>www.jb51.net</p>
</div>
<p>是国内专业的网站建设资源.</p>
</div>
<div class="box">
<h2><input type="button" value="点击淡出" /></h2>
<div id="fadeOut">
<p></p>
<p>www.jb51.net</p>
</div>
<p>是国内专业的网站建设资源.</p>
</div>
<div class="box">
<h2><input type="button" value="点击淡出至指定透明度" /></h2>
<div id="fadeTo">
</div>
<p>是国内专业的网站建设资源.</p>
</div>
</div>
<!--DEMO end-->
</body>
</html>
以上就是本文的全部内容,希望对大家学习原生js和jQuery实现淡入淡出轮播效果有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“原生js和jQuery实现淡入淡出轮播效果”评论...
更新日志
2025年10月29日
2025年10月29日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]