javascript - 悬停时删除 clipPath

标签 javascript jquery html css svg

简而言之,我有一个使用 clipPath 屏蔽的图像,因此它可以在 IE 9+ 中工作。问题是我需要在悬停时隐藏蒙版以显示完整图像,然后在鼠标移开时重新应用。我现在拥有的脚本不起作用。笔在下面。我对 SVG 和 clipPath 很陌生。

http://codepen.io/OMGDrAcula/pen/eJPzQx

$(document).ready(function() {
  $('.finish')
    .mouseover(function() {
      $(this).find('svg').find('clipPath').css('display', 'none');
    }).mouseout(function() {
      $(this).find('svg').find('clipPath').css('display', 'block');
    });
})
<div class="col-xs-3 finish" style="position:relative;border:1px solid red;">
  <img src="http://placehold.it/297x252" class="img-responsive" />
  <svg preserveAspectRatio="xMidYMin slice" style="width:100%;height:252px;top:0;left:0;position:absolute;border:dotted 2px blue" version="1.1" xmlns="http://www.w3.org/2000/svg" width="297" height="252" x="0px" y="0px" style="enable-background:new 0 0 297 252;"
  viewBox="0 0 297 252" xml:space="preserve">
    <defs>
      <clipPath id="maskID0">
        <rect width="100%" height="252" x="0" y="0" />
      </clipPath>
    </defs>
    <title>Test Image</title>
    <desc>Test 123</desc>
    <image clip-path="url(#maskID0)" width="297" height="252" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://placehold.it/297x252"></image>
  </svg>
</div>

最佳答案

更改 clipPath 元素的 display 属性本身没有任何效果,因为 clipPath 只是一个定义,它没有任何自己的展示。它是由定义的 clipPath 裁剪后显示的 image。因此,一个简单的解决方案是在鼠标移入时删除 clip-path 属性,然后在鼠标移出时再次添加它(连同原始剪辑路径 URL)。

$(document).ready(function() {
  $('.finish').mouseover(function() {
    $(this).find('svg image').removeAttr('clip-path');
  }).mouseout(function() {
    $(this).find('svg image').attr('clip-path', 'url(#maskID0)');
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-xs-3 finish" style="position:relative;border:1px solid red;">
  <img src="http://placehold.it/297x252" class="img-responsive" />
  <svg preserveAspectRatio="xMidYMin slice" style="width:100%;height:252px;top:0;left:0;position:absolute;border:dotted 2px blue" version="1.1" xmlns="http://www.w3.org/2000/svg" width="297" height="252" x="0px" y="0px" style="enable-background:new 0 0 297 252;"
  viewBox="0 0 297 252" xml:space="preserve">
    <defs>
      <clipPath id="maskID0">
        <rect width="150" height="252" x="0" y="0" />
      </clipPath>
    </defs>
    <title>Test Image</title>
    <desc>Test 123</desc>
    <image clip-path="url(#maskID0)" width="297" height="252" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://placehold.it/297x252"></image>
  </svg>
</div>

关于javascript - 悬停时删除 clipPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35229532/

相关文章:

html - css 动画边之间有空格

javascript - 在开发控制台中触发时,JQuery "click"事件不会触发

javascript - 最优雅的图像到图像帧比较算法

javascript - 带有 React 的语义 UI 模态组件 onClose

jquery - 第一个过渡不平滑 jQuery 淡入/淡出

c# - 使用 Javascript 更改级联 DropDownList 值时 MVC 模型中断

html - 如何并排放置 3 个 div 并将每个 div 的内容居中

javascript - 如何使用 jsdoc 记录函数生成器?

javascript - Php 不从 json 发布数据,请

javascript - 如何订购运行 ajax 的 jQuery 然后必须返回 true