利用JavaScript查看照片

刚入门学习js,主要是兴趣使然,想做一个打开任意的文件夹,显示里面的照片文件,由于js不能获取绝对路径,使用

FileReader进行显示照片,具体代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>读取文件显示图片</title>
</head>
<style>
		*{
			margin: 0px;
			padding: 0px;
		}
		img{
		    width:100px;
		    height:500px;
            min-height: 100%;
            width: auto;
			max-width:100%;
			max-height:100%;

		}
		button{
		width: 100px;
        height: 30px;
        border-radius: 5px;
        background: green;
        color: #fff;
        margin:12%;
        margin-top: 1%;

		}


</style>



<body>
<br>
<br>
<br>
<br>

<br>
<input type="file" id="fileInput" onchange="selectFolder()" webkitdirectory directory multiple>
<div style="text-align:center">
  <span style="color:blue;font-size:20px;" id="sp"><b></b></span>
</div>
<br>
<div class="container" style="text-align:center">
    <img id="pic" src="">
    <br>
    <button id='prev' onclick="prev()">上一张</button>
    <button id='next' onclick="next()">下一张</button>
</div>
<div id="result"></div>

<script language="javascript">
    //读取图片类型
    function selectFolder() {
    var inputBox = document.getElementById("fileInput");
    var index = 0;
    var imgext = new Array("png","jpg","jpeg","bmp","gif");
    var files = inputBox.files;
    var ss = [];
    for (var i = 0; i < files.length; i++) {
    //判断是不是图片文件,不是的话不存入ss
        var ind= files[i].name.split(".")[1];
        //判断是不是包含图片文件
        if (imgext.includes(ind)){
            console.log(files[i]);
            ss.push(files[i]);
            }

    }

    //将局部变量ss变为全局变量
    globalss = ss;
    globalindex = index;
    if (ss.length==0){
        alert('没有照片文件!')
    }
    else{
        document.getElementById("sp").innerHTML = 1+"/"+globalss.length;
        var reader = new FileReader();
        globalreader = reader;
        reader.readAsDataURL(ss[0]);//发起异步请求
        reader.onload = function()
        {
            pic.src = this.result;
            //读取完成后,数据保存在对象的result属性中this.result就是reader.readAsDataURL(ss[0])
            console.log(this.result)

        }

}

   }



        function prev(){

            //判断index是不是大于0
            if (globalindex>0){
            globalindex = globalindex-1

            globalreader.readAsDataURL(globalss[globalindex]);
            document.getElementById("pic").src=this.result;
            document.getElementById("sp").innerHTML = globalindex+1+"/"+globalss.length;
            }
            else{
            alert('这是第一张图片!')
            }
        }

    function next(){
            globalindex = globalindex+1
            //alert(index);
            //判断index是不是大于ss的长度
            if (globalindex>=globalss.length){
            alert('已经是最后一张图片了!')
            }
            else{
            globalreader.readAsDataURL(globalss[globalindex]);
            document.getElementById("pic").src=this.result;
            document.getElementById("sp").innerHTML = globalindex+1+"/"+globalss.length;
            }
        }



</script>


</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要制作动画照片墙,可以使用CSS3中的transition和transform属性,以及JavaScript中的事件监听和DOM操作。 首先,将照片以img标签的形式嵌入到HTML文档中,并添加类名以便于选择和操作。然后,使用CSS3的flexbox布局将照片排列成网格状,并设置每个照片的宽度、高度和边距。 接下来,为每个照片添加鼠标悬停事件监听,当鼠标悬停在某个照片上时,使用CSS3的transform属性将其放大,并使其z-index属性增加,使其浮在其他照片的上方。同时,设置transition属性使其具有缓慢过渡的效果。 最后,使用JavaScript获取所有照片元素,并为其添加事件监听,当鼠标离开任意照片时,将其恢复原始大小和层级。 以下是示例代码: HTML: ```html <div class="photo-wall"> <img src="photo1.jpg" class="photo"> <img src="photo2.jpg" class="photo"> <img src="photo3.jpg" class="photo"> <img src="photo4.jpg" class="photo"> <img src="photo5.jpg" class="photo"> <img src="photo6.jpg" class="photo"> <img src="photo7.jpg" class="photo"> <img src="photo8.jpg" class="photo"> </div> ``` CSS: ```css .photo-wall { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; } .photo { width: 200px; height: 200px; margin: 10px; object-fit: cover; transition: all 0.3s ease; } .photo:hover { transform: scale(1.2); z-index: 1; } ``` JavaScript: ```javascript const photos = document.querySelectorAll('.photo'); photos.forEach(photo => { photo.addEventListener('mouseleave', () => { photo.style.transform = 'scale(1)'; photo.style.zIndex = '0'; }); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值