轮播图的实现

第一步javaee fristpage.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>

首页

第二步js样式autoplay.js

//获取ID
var $ = function (id) {return typeof id === “string” ? document.getElementById(id) : id;};
//获取tagName
var KaTeX parse error: Can't use function '$' in math mode at position 244: … this.oBox = $̲(id); this.oU…(“ul”, this.oBox)[0];
this.aImg = ( &quot; i m g &quot; , t h i s . o B o x ) ; t h i s . t i m e r = n u l l ; t h i s . a u t o T i m e r = n u l l ; t h i s . i N o w = 0 ; t h i s . c r e a t B t n ( ) ; t h i s . a B t n = (&quot;img&quot;, this.oBox); this.timer = null; this.autoTimer = null; this.iNow = 0; this.creatBtn(); this.aBtn = ("img",this.oBox);this.timer=null;this.autoTimer=null;this.iNow=0;this.creatBtn();this.aBtn=(“li”, this.oCount);
this.toggle();
this.autoTimer = setInterval(function ()
{
oThis.next();
}, 3000);
this.oBox.onmouseover = function ()
{
clearInterval(oThis.autoTimer);
};
this.oBox.onmouseout = function ()
{
oThis.autoTimer = setInterval(function ()
{
oThis.next();
}, 3000);
};
for (var i = 0; i < this.aBtn.length; i++)
{
this.aBtn[i].index = i;
this.aBtn[i].onmouseover = function ()
{
oThis.iNow = this.index;
oThis.toggle();
};
}
},
creatBtn: function ()
{
this.oCount = document.createElement(“ul”);
this.oFrag = document.createDocumentFragment();
this.oCount.className = “count”;
for (var i = 0; i < this.aImg.length; i++)
{
var oLi = document.createElement(“li”);
oLi.innerHTML = i + 1;
this.oFrag.appendChild(oLi);
}
this.oCount.appendChild(this.oFrag);
this.oBox.appendChild(this.oCount);
},
toggle: function ()
{
for (var i = 0; i < this.aBtn.length; i++) this.aBtn[i].className = “”;
this.aBtn[this.iNow].className = “current”;
this.doMove(-(this.iNow * this.aImg[0].offsetHeight));
},
next: function ()
{
this.iNow++;
this.iNow == this.aBtn.length && (this.iNow = 0);
this.toggle();
},
doMove: function (iTarget)
{
var oThis = this;
clearInterval(oThis.timer);
oThis.timer = setInterval(function ()
{
var iSpeed = (iTarget - oThis.oUl.offsetTop) / 5;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
oThis.oUl.offsetTop == iTarget ? clearInterval(oThis.timer) : (oThis.oUl.style.top = oThis.oUl.offsetTop + iSpeed + “px”);
}, 30);
}
};
window.onload = function ()
{
new AutoPlay(“box_autoplay”);
};

第三步css样式autoplay.css

body, div, ul, li {
margin:0;
padding:0;
}
ul {
list-style-type:none;
}

#box_autoplay {
position:relative;
width:900px;
height:335px;
background:#fff;
border-radius:5px;
border:8px solid #fff;
margin:10px auto;
cursor:pointer;
}
#box_autoplay .list {
position:relative;
width:900px;
height:335px;
overflow:hidden;
}
#box_autoplay .list ul {
position:absolute;
top:0;
left:0;
}
#box_autoplay .list li {
width:900px;
height:335px;
overflow:hidden;
}
#box_autoplay .count {
position:absolute;
right:0;
bottom:5px;
}
#box_autoplay .count li {
color:#fff;
float:left;
width:20px;
height:20px;
cursor:pointer;
margin-right:5px;
overflow:hidden;
background:#F90;
opacity:0.7;
filter:alpha(opacity=70);
border-radius:20px;
}
#box_autoplay .count li.current {
color:#fff;
opacity:1;
filter:alpha(opacity=100);
font-weight:700;
background:#f60;
}
#tmp {
width:100px;
height:100px;
background:red;
position:absolute;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 轮实现可以使用 ViewPager 或者第三方库,这里以 ViewPager 为例: 1. 首先在布局文件中添加 ViewPager 和指示器(Indicator): ``` <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="200dp"/> <LinearLayout android:id="@+id/indicator" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center" android:orientation="horizontal"/> ``` 2. 创建一个 Adapter 类,用于管理轮的数据和布局: ``` public class BannerAdapter extends PagerAdapter { private Context mContext; private List<Integer> mImages; public BannerAdapter(Context context, List<Integer> images) { mContext = context; mImages = images; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(mContext); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setImageResource(mImages.get(position)); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public int getCount() { return mImages.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } ``` 3. 在 Activity 或 Fragment 中设置 ViewPager 和指示器: ``` ViewPager viewPager = findViewById(R.id.viewPager); LinearLayout indicatorLayout = findViewById(R.id.indicator); BannerAdapter adapter = new BannerAdapter(this, images); viewPager.setAdapter(adapter); for (int i = 0; i < images.size(); i++) { ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.indicator_selector); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(10, 0, 10, 0); indicatorLayout.addView(imageView, params); } viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageSelected(int position) { for (int i = 0; i < indicatorLayout.getChildCount(); i++) { ImageView imageView = (ImageView) indicatorLayout.getChildAt(i); imageView.setSelected(i == position); } } @Override public void onPageScrollStateChanged(int state) {} }); ``` 这样就完成了一个简单的 Android 轮实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值