CSS3感知滑鼠進入方向

螞蟻小編發表於2018-07-02

以前實現滑鼠進入方位的感知功能,需要結合javascript來實現。

css3的普遍應用又提供了一個新的方式。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
.antzone {
  width: 1000px;
  height: auto;
  overflow: hidden;
  cursor: pointer;
}
.antzone [fox=con] {
  width: calc((990px - 10px * 3) / 4);
  background: url(demo/CSS/img/shangpin.jpg) -616px 0 no-repeat;
  height: calc((990px - 10px * 3) / 4 / 3 * 4);
  position: relative;
  overflow: hidden;
  margin: 10px 10px 0 0;
  float: left;
}
.antzone [fox=con] [fox$=_c]:nth-of-type(2n-1) {
  width: 240px;
  height: 240px;
  transform: rotate(45deg) skew(8deg,8deg);
  position: absolute;
  left: 2px;
}
.antzone [fox=con] [fox$=_c]:nth-of-type(2n) {
  width: 240px;
  height: 240px;
  transform: rotate(45deg) skew(8deg,8deg);
  position: absolute;
  top: 42px;
}
.antzone [fox^=top] {
  top: -151px;
}
.antzone [fox^=right] {
  left: 148px;
}
.antzone [fox^=bottom] {
  bottom: -151px;
}
.antzone [fox^=left] {
  right: 147px;
}
.antzone [fox=con]:hover [fox$=_c] {
  transform: none;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
.antzone [fox=con] [fox$=_c]:hover {
  z-index: 5;
}
.antzone [fox$=_c] [fox$=_con] {
  display: block;
  width: calc((990px - 10px * 3) / 4);
  height: calc((990px - 10px * 3) / 4 / 3 * 4);
  background-image: linear-gradient(0deg,#4fc4b2,#fff,#b0fef2);
}
.antzone [fox^=top] [fox$=_con] {
  transform: translateY(-100%);
}
.antzone [fox^=bottom] [fox$=_con] {
  transform: translateY(100%);
}
.antzone [fox^=left] [fox$=_con] {
  transform: translateX(-100%);
}
.antzone [fox^=right] [fox$=_con] {
  transform: translateX(100%);
}
.antzone [fox$=_c]:hover [fox$=_con] {
  transform: translateY(0);
  transition: all .3s linear;
  transform: translateX(0);
}
</style>
</head>
<body>
  <div>
    <div style="width:990px;margin:0 auto;">
      <div class="antzone">
        <div fox="con">
          <div fox="top_c"><a fox="con_con"></a></div>
          <div fox="right_c"><a fox="con_con"></a></div>
          <div fox="bottom_c"><a fox="con_con"></a></div>
          <div fox="left_c"><a fox="con_con"></a></div>
        </div>
        <div fox="con">
          <div fox="top_c"><a fox="con_con"></a></div>
          <div fox="right_c"><a fox="con_con"></a></div>
          <div fox="bottom_c"><a fox="con_con"></a></div>
          <div fox="left_c"><a fox="con_con"></a></div>
        </div>
        <div fox="con">
          <div fox="top_c"><a fox="con_con"></a></div>
          <div fox="right_c"><a fox="con_con"></a></div>
          <div fox="bottom_c"><a fox="con_con"></a></div>
          <div fox="left_c"><a fox="con_con"></a></div>
        </div>
        <div fox="con">
          <div fox="top_c"><a fox="con_con"></a></div>
          <div fox="right_c"><a fox="con_con"></a></div>
          <div fox="bottom_c"><a fox="con_con"></a></div>
          <div fox="left_c"><a fox="con_con"></a></div>
        </div>
        <div fox="con">
          <div fox="top_c"><a fox="con_con"></a></div>
          <div fox="right_c"><a fox="con_con"></a></div>
          <div fox="bottom_c"><a fox="con_con"></a></div>
          <div fox="left_c"><a fox="con_con"></a></div>
        </div>
        <div fox="con">
          <div fox="top_c"><a fox="con_con"></a></div>
          <div fox="right_c"><a fox="con_con"></a></div>
          <div fox="bottom_c"><a fox="con_con"></a></div>
          <div fox="left_c"><a fox="con_con"></a></div>
        </div>
        <div fox="con">
          <div fox="top_c"><a fox="con_con"></a></div>
          <div fox="right_c"><a fox="con_con"></a></div>
          <div fox="bottom_c"><a fox="con_con"></a></div>
          <div fox="left_c"><a fox="con_con"></a></div>
        </div>
        <div fox="con">
          <div fox="top_c"><a fox="con_con"></a></div>
          <div fox="right_c"><a fox="con_con"></a></div>
          <div fox="bottom_c"><a fox="con_con"></a></div>
          <div fox="left_c"><a fox="con_con"></a></div>
        </div>
      </div>
    </div>
  </div>
  </body>
</html>

相關文章