DDR爱好者之家 Design By 杰米

自己用原生js写的简单焦点图,算是对原生js初步运用。

原生js编写焦点图效果

html部分

<div class="focusPicture">
          <section class="mPhoto" id="mPhoto">
            <img src="/UploadFiles/2021-04-02/b_5809b93b66d18.jpg">

js部分

window.onload = function(){


 /*焦点图特效*/
 var mPhoto = document.getElementById("mPhoto");
 var imgWrap = document.getElementsByClassName("imgWrap")[0];
 var wrapSize = 4; /*图片容器最大显示图片数*/
 if(imgWrap.getElementsByTagName("section").length>wrapSize){
  imgWrap.innerHTML = imgWrap.innerHTML+imgWrap.innerHTML;
 }
 
 var oImages = imgWrap.getElementsByTagName("section");
 for(var i = 0 ; i<oImages.length; i++)
 {
  oImages[i].onmouseover = function()
  {
   for(var j = 0 ; j<oImages.length; j++)
   {
    removeClass(oImages[j],"on");
   }
   var imgSrc = this.getElementsByTagName("img")[0].src;
   addClass(this,"on");
   mPhoto.getElementsByTagName("img")[0].src = imgSrc;
  }
 }


 /*上下翻页容器*/
 var oPrevNext = document.getElementsByClassName("prevNext")[0];
 var oPrev = oPrevNext.getElementsByClassName("prev")[0];
 var oNext = oPrevNext.getElementsByClassName("next")[0];
 var oImageHeight = oImages[0].getElementsByTagName("img")[0].height||0;
 var oPositionTop = imgWrap.offsetTop;
 var imgIndex = 0;
 
 if(oImages.length>wrapSize*2)
 {
  oPrev.style.display = "block";
  oNext.style.display = "block";
 }
 
 /*上一页*/
 oPrev.onclick=function()
 {
  imgIndex-=1;
  if(imgIndex<=0){
   imgIndex = 6;
  }


  var oTop = imgIndex*(oImageHeight+8);
  imgWrap.style.top = -oTop+"px";
  
  /*获取当前图片的索引*/
  for(var i = 0 ; i<oImages.length; i++)
  {
   var oBol = hasClass(oImages[i],"on")||false;
   if(oBol){
    var index = oIndex(oImages[i],oImages);
    index=index-1;
    if(imgIndex==6){
     index = 6;
    }
    for(var j = 0 ; j<oImages.length; j++)
    {/*清除其他的图片on样式*/
     removeClass(oImages[j],"on");
    }
    addClass(oImages[index],"on");
    var imgSrc = oImages[index].getElementsByTagName("img")[0].src;
    mPhoto.getElementsByTagName("img")[0].src = imgSrc;


    return false;
   }
  }
 }
 
 /*下一页*/
 oNext.onclick = function()
 {
  imgIndex+=1;
  if(imgIndex>=6){
   imgIndex = 0;
  }
  var oTop = imgIndex*(oImageHeight+8);
  imgWrap.style.top = -oTop+"px";
  
  /*获取当前图片的索引*/
  for(var i = 0 ; i<oImages.length; i++)
  {
   var oBol = hasClass(oImages[i],"on")||false;
   if(oBol){
    var index = oIndex(oImages[i],oImages);
    index=index+1;
    if(imgIndex==0){
     index = 1;
    }
    for(var j = 0 ; j<oImages.length; j++)
    {/*清除其他的图片on样式*/
     removeClass(oImages[j],"on");
    }
    addClass(oImages[index],"on");
    var imgSrc = oImages[index].getElementsByTagName("img")[0].src;
    mPhoto.getElementsByTagName("img")[0].src = imgSrc;


    return false;
   }
  }


 }
}


function hasClass(element,cName)
{//检查class是否存在
 return !!element.className.match(new RegExp('(\\s|^)'+cName+'(\\s|$)'));
}
function addClass(element,cName)
{//添加一个class
 if(!hasClass(element,cName))
 {
  element.className +=' '+cName;
 }
}
function removeClass(element,cName)
{//移除一个class
 if(hasClass(element,cName))
 {
  element.className = element.className.replace(new RegExp('(\\s|^)'+cName+'(\\s|$)'),' ');
 }
}


function oIndex(current, obj)
{/*获取索引*/
 for (var i = 0, length = obj.length; i<length; i++)
 { 
  if (obj[i] == current)
  { 
   return i; 
  } 
 } 
}

css部分

.productInfo .focusPicture{
 width: 698px;
 float: left;
 position: relative;
 border-right: 1px solid #d9cdb4;
}
.productInfo .focusPicture .mPhoto{
 width: 542px;
 height: 360px;


}
.productInfo .focusPicture .mPhoto img{
 width: 100%;
 height: 100%;
}


.productInfo .focusPicture .oPhoto{
 width: 128px;
 height: 352px;
 position: absolute;
 right: 14px;
 top: 4px;
 overflow: hidden;


}


.productInfo .oPhoto div{
 width: 128px;
 position: absolute;
 left:0;
 top:0;
 -webkit-transition: top .2s ease-out;
 -moz-transition: top .2s ease-out;
 transition: top .2s ease-out; 
}
.productInfo .oPhoto section{
 height: 78px;
 margin: 8px 0;
 position: relative;
 cursor: pointer;
}
.productInfo .oPhoto section::before {
 content: "";
 display: block;
 width: 100%;
 height: 100%;
 background-color: rgba(255,255,255,.42);
 position: absolute;
 left: 0;
 top: 0;
}


.productInfo .oPhoto section.on::before{
 display: none;
}


.productInfo .focusPicture .oPhoto img{
 width: 100%;
 height: 100%;
}


.productInfo .focusPicture .prevNext{
 width: 32px;
 height: 32px;
 position: absolute;
 top: -4px;
 right: 62px;
}


.productInfo .prevNext div.prev,
.productInfo .prevNext div.next{
 width: 32px;
 height: 32px;
 -webkit-border-radius: 50%;
 -moz-border-radius: 50%;
 border-radius: 50%;
 background-color: #fff;
 position: absolute;
 left: 0;
 -webkit-transition: all .25s;
 -moz-transition: all .25s;
 transition: all .25s;
 display: none;
 cursor: pointer;
}
.productInfo .prevNext div.prev{
 top: 0;
 box-shadow: 0 -2px 4px rgba(0,0,0,.24);


}
.productInfo .prevNext div.next{
 top: 336px;
 box-shadow: 0 2px 4px rgba(0,0,0,.24);
}


.productInfo .prevNext div.prev:hover{
 box-shadow: 2px -2px 3px rgba(0,0,0,.24);


}
.productInfo .prevNext div.next:hover{
 box-shadow: -2px 2px 3px rgba(0,0,0,.24);
}


.productInfo .prevNext div.prev i,
.productInfo .prevNext div.next i{
 width: 16px;
 height: 16px;
 position: absolute;
 top: 0;
 right: 0;
 bottom: 0;
 left: 0;
 margin: auto;
 cursor: pointer;
}
.productInfo .prevNext div.prev i{
 background: url(../images/arrows-icon.png) 0 -16px no-repeat;
 background-size: 48px 32px;
 bottom: 2px;
}


.productInfo .prevNext div.next i{
 background: url(../images/arrows-icon.png) 0 0 no-repeat;
 background-size: 48px 32px;
 top: 2px;
}


.productInfo .prevNext div.prev:hover i,
.productInfo .prevNext div.next:hover i{
 -webkit-transform: scale(1.1);
 -moz-transform: scale(1.1);
 transform: scale(1.1);
}


.productInfo .prevNext div.prev:hover i{
 background: url(../images/arrows-icon.png) -16px -16px no-repeat;
 background-size: 48px 32px;
}


.productInfo .prevNext div.next:hover i{
 background: url(../images/arrows-icon.png) -16px 0px no-repeat;
 background-size: 48px 32px;
}


.productInfo .prevNext div.prev:active i{
 background: url(../images/arrows-icon.png) -32px -16px no-repeat;
 background-size: 48px 32px;
}


.productInfo .prevNext div.next:active i{
 background: url(../images/arrows-icon.png) -32px 0px no-repeat;
 background-size: 48px 32px;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。