简体   繁体   English

jQuery Border Image(左上,右上,左下,右下)

[英]jQuery Border Image (top-left, top-right, bottom-left, bottom-right)

I'm building a website and when the user hovers over a product I want to give that image a border of the top-left, top-right, bottom-left and bottom-right. 我正在建立一个网站,当用户将鼠标悬停在某个产品上时,我想给该图像添加一个左上,右上,左下和右下的边框。 As the CSS properties border-top-left-image were deprecated years ago the only other solution to this would be to use JS. 由于几年前不赞成使用CSS属性border-top-left-image ,因此唯一的解决方案是使用JS。 Currently my idea is that I'll use span with an icon class and then append the four spans on hover then remove them from the DOM, is this the best solution for this or is there something simpler where I'm not appending four spans and removing them every time a product is hovered, here's my current code tell me what you think and any advice would be great, thanks in advance! 目前,我的想法是我将span与图标类一起使用,然后将四个span附加在悬停上,然后将它们从DOM中删除,这是最好的解决方案,还是有一些更简单的方法,我不附加四个span和每次将产品悬停时都将其删除,这是我当前的代码,告诉我您的想法,任何建议都是对的,谢谢!

CSS CSS

.icon {
    background: url("../images/theme/icons.png");
    overflow: hidden;
    width: 1.6em;
    height: 1.6em;
    position: absolute; 
}


.top-left {
    top: 0; 
    left: 0;
    background-position: -11.2em 24em;
}

.top-right { 
    top: 0;
    right: 0;
    background-position: -1.6em 24em;
}

.bottom-left { 
    bottom: 0;
    left: 0;
    background-position: -8em 24em;
}

.bottom-right { 
    bottom: 0;
    right: 0;
    background-position: -4.8em 24em;
}

HTML HTML

<div class="layout-product">
    <a href="http://www.mysite.com/product/lorem-ipsum-1">
        <div class="product-image">
             <img src="http://www.mysite.com/images/thumbnail/lorem-ipsum-1.jpg" alt="">
        </div>
    </a>
</div>

jQuery jQuery的

$('.layout-product').hover(function() { 

    $(this).find('.product-image').append('<span class="icon top-left"></span>'

        + '<span class="icon top-right"></span>'
        + '<span class="icon bottom-left"></span>'
        + '<span class="icon bottom-right"></span>'
    );

}, function() {

    $('.icon').remove();
});

You should try to re-use nodes before creating/removing them. 您应该在创建/删除节点之前尝试重用它们。 The less JavaScript has to engage with the DOM the faster, and hance smoother your animation and the UX will be. JavaScript与DOM交互所花费的时间越少,并且动画和UX就会越平滑。 Since you're not retrieving any dynamic information on the hover event you can just put the .icon elements into your initial html, with a display: none property. 由于您没有获取有关悬停事件的任何动态信息,因此您可以将.icon元素放入display: none初始html中。 And either in CSS or using JS show/hide on hover. 并且在CSS中或在悬停时使用JS show / hide。 Also scripts block page load, so to an extent they impact performance, obviously depending on the length of your script. 脚本还会阻止页面加载,因此在一定程度上会影响性能,这显然取决于脚本的长度。

Eg. 例如。

(Adding to your existing CSS) (添加到现有的CSS中)

/* Default */
.layout-product .icon {
  display: none;
}

.layout-product:hover .icon {
  display: block; /* or inline-block or whatever you like that isn't none */
}

HTML HTML

<div class="layout-product">
  <a href="http://www.mysite.com/product/lorem-ipsum-1">
    <div class="product-image">
      <img src="http://www.mysite.com/images/thumbnail/lorem-ipsum-1.jpg" alt="">
      <span class="icon top-left"></span>
      <span class="icon top-right"></span>
      <span class="icon bottom-left"></span>
      <span class="icon bottom-right"></span>
    </div>
  </a>
</div>

If you would like greater flexibility with your animation you could use Javascript instead of the CSS I provided. 如果您希望动画具有更大的灵活性,可以使用Javascript 代替我提供的CSS。

JavaScript JavaScript的

// I have used a toggle which means it applies for both .mouseenter and .mouseleave 
// which the .hover function alias. If you pass one function rather than two it will
// Use that function for both events. Customize as you like.
$('.layout-product').hover(function(e) { 

  $(this).find('.icon').fadeToggle();

});

Also it goes without saying if you could avoid the use of empty elements in your html and use whichever css properties you can, either background of border-image as you mentioned, then that would be preferential, less DOM elements is better performance and maintainability, and purely presentational elements should be avoided where possible for semantics (separation of Presentation from Content). 同样不用说,如果您可以避免在html中使用空元素,而可以使用任何css属性(如您提到的border-image background ,那将是优先选择的,较少的DOM元素具有更好的性能和可维护性,并且应尽可能避免使用语义上的纯粹表示元​​素(表示与内容分离)。

Browser compatibility is obviously going to be a factor: 浏览器兼容性显然是一个因素:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery:排序4点到(左上,右上,右下,左下) - jquery: sort 4 points to (top-left, top-right, bottom-right, bottom-left) 如何在mousemove事件(左上,右上,左下和右下)上确定鼠标的方向 - How to determine the direction of mouse on mousemove event (top-left, top-right, bottom-left and bottom-right) react-spring:从左上角到右下角淡入淡出 - react-spring: fade in top-left to bottom-right 如何从边界值中提取左上角和右下角坐标 - How to extract top-left and bottom-right coordinates from bounds value 给定以矩形左下角为中心的圆,计算圆的边界矩形的宽度,使其半径接触右上角 - Given circle that is centered on bottom-left corner of a rect, calculate width of circle's bounding rect so its radius touches the top-right corner Galleria滑块从左到右从上到下 - Galleria slider from left—right to top—bottom getBoundingClientRect 在 Safari 中为顶部、左侧、右侧、底部返回 0 - getBoundingClientRect return 0 for top, left, right, bottom in Safari 绝对定位,负顶部/左/右/底部用于图像拉伸 - Absolute positioning with negative top/left/right/bottom for image stretching 纯JavaScript从左,上,右,下切割图像 - Pure JavaScript cut image from left, top, right, bottom jQuery UI Position设置为“ right”和“ bottom”,而不是“ left”和“ top” - jQuery UI Position to set “right” and “bottom” instead of “left” and “top”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM