html页面点击小图弹出大图代码,js图片画廊点击弹出遮罩背景大图切换代码

一款简单实用的js图片画廊点击弹出遮罩背景大图切换代码,点击小图弹出大图幻灯片展示,左右按钮控制图片上一张下一张进行切换。

3d3e3e70838fe46bdefa3e998f79ce51.png

查看演示

下载资源:

34

次 下载资源

下载积分:

20

积分

js代码

(function(){

var LightBox = function(options){

this.imgListParent = document.getElementById(options.imgListParent); //图片列表的父元素

this.imgItemClass = options.imgItemClass; //图片的className

this.idx = 0; //图片的索引,初始值为0

this.isShowPage = options.isShowPage || false; //是否显示分页,默认不显示

this.init();

};

//初始化

LightBox.prototype.init = function(){

this.renderDOM();

this.imgListClick();

this.nextBtnClick();

this.prevBtnClick();

this.closeBtnClick();

};

//渲染弹窗

LightBox.prototype.renderDOM = function(){

var imgModule = document.createElement("div");

imgModule.id = "imgModule";

var oHtml = "";

oHtml += '

oHtml += '

';

imgModule.innerHTML = oHtml;

document.body.appendChild(imgModule);

};

//分页

LightBox.prototype.pagination = function(idx){

var imgList = this.getByClass(this.imgListParent, this.imgItemClass);

var pagination = document.getElementById("lightBoxPagination");

var page = "";

for(var i = 0; i < imgList.length; i++){

if(idx == i){

page += '';

}else{

page += '';

}

}

if(this.isShowPage){

pagination.innerHTML = page;

}

};

//点击图片弹出弹窗

LightBox.prototype.imgListClick = function(){

var imgList = this.getByClass(this.imgListParent, this.imgItemClass);

var imgModule = document.getElementById("imgModule");

var self = this;

for(var i = 0; i < imgList.length; i++){

imgList[i].index = i;

imgList[i].onclick = function(){

imgModule.style.display = "block";

var src = this.getAttribute("data-src");

self.idx = this.index;

self.imgLoad(src);

self.pagination(self.idx);

}

}

};

//上一张

LightBox.prototype.prevBtnClick = function(){

var prevBtn = document.getElementById("lightBoxPrev");

var self = this;

prevBtn.onclick = function(){

var imgList = self.getByClass(self.imgListParent, self.imgItemClass);

self.idx--;

if(self.idx < 0){

self.idx = imgList.length - 1;

}

var src = imgList[self.idx].getAttribute("data-src");

self.imgLoad(src);

self.pagination(self.idx);

}

};

//下一张

LightBox.prototype.nextBtnClick = function(){

var nextBtn = document.getElementById("lightBoxNext");

var self = this;

nextBtn.onclick = function(){

var imgList = self.getByClass(self.imgListParent, self.imgItemClass);

self.idx++;

if(self.idx >= imgList.length){

self.idx = 0;

}

var src = imgList[self.idx].getAttribute("data-src");

self.imgLoad(src);

self.pagination(self.idx);

}

};

//图片预加载

LightBox.prototype.imgLoad = function(src, callback){

var imgLight = document.getElementById("imgLight");

var loader = document.getElementById("imgLoader");

loader.style.display = "block";

// imgLight.src = "";

var img = new Image();

img.onload = function(){

loader.style.display = "none";

imgLight.src = src;

};

img.src = src;

};

//关闭弹窗

LightBox.prototype.closeBtnClick = function(){

var closeBtn = document.getElementById("closeBtn");

var imgModule = document.getElementById("imgModule");

closeBtn.onclick = function(){

imgModule.style.display = "none";

}

};

//封装获取元素函数

LightBox.prototype.getByClass = function(oParent, oClass){

var oEle = oParent.getElementsByTagName("*");

var oResult = [];

for(var i = 0; i < oEle.length; i++){

if(oEle[i].className == oClass){

oResult.push(oEle[i]);

}

}

return oResult;

};

window.LightBox = LightBox;

})();

new LightBox({

imgListParent: "imgDefault",

imgItemClass: "imgItem",

isShowPage: true

});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码来实现弹出PopupWindow,并添加背景遮罩以及点击遮罩时弹窗消失。 首先,在你的布局文件(例如activity_main.xml)中添加一个透明的背景遮罩视图: ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Your main layout content here --> <View android:id="@+id/background_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" /> </FrameLayout> ``` 然后,在你的Activity或Fragment中的代码中,添加以下方法来显示和隐藏PopupWindow: ```java public class MainActivity extends AppCompatActivity { private PopupWindow popupWindow; private View backgroundView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); backgroundView = findViewById(R.id.background_view); // 设置背景遮罩点击事件 backgroundView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismissPopupWindow(); } }); // 显示PopupWindow showPopupWindow(); } private void showPopupWindow() { // 创建PopupWindow视图 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View popupView = inflater.inflate(R.layout.popup_window, null); // 设置PopupWindow的属性 popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示PopupWindow popupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0); } private void dismissPopupWindow() { // 隐藏PopupWindow if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } } } ``` 最后,创建一个popup_window.xml资源文件来定义PopupWindow的布局: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical" android:padding="16dp"> <!-- Your PopupWindow content here --> </LinearLayout> ``` 这样,当你的Activity或Fragment启动时,PopupWindow将显示在屏幕中央,并且点击背景遮罩时PopupWindow将被隐藏。请根据你的需要修改布局和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值