Responsive Image Hover - CSS/JQuery

一曲冷凌霜 提交于 2020-01-11 13:51:06

问题


I followed this tutorial (http://mattbango.com/notebook/code/hover-zoom-effect-with-jquery-and-css/) and built out a image-hover plugin that I'd like to use on my site.

The only problem I have is that I want it to work responsively, scaling the images according to my 960gs.

Here's the fiddle I have so far: http://jsfiddle.net/Ak94R/6/

.viewport {
    float: left;
    height: 360px;
    margin: 0 9px 9px 0;
    overflow: hidden;
    position: relative;
    width: 360px;

}

Instead of shrinking that image from 730px/730px to 360px/360px, I want to shrink it from 200% to 100%. I also need the main clipping div (.viewport) to be sized at 100%. Any help would be greatly appreciated!


回答1:


Ok, I've just spend a few minutes for a CSS only solution. Does the same thing and no JS required. Fully responsive since it works with percentages. The HTML looks like this:

<div class="viewport_css">
    // I have to use a dummy image to force dimensions
    <img class="dummy" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" />
    <a class="imgwrapper" href="#">
        <img src="http://www.lorempixel.com/730/730/" alt="More Info" />
    </a>
</div>

The CSS part:

.viewport_css {
    position: relative;
    max-width: 360px;
    height: auto;
    overflow: hidden;
}

// make sure viewport_css always is square shaped
.viewport_css .dummy {
    width: 100%;
    height: auto;
    display: block;
}

.viewport_css a,
.viewport_css a:hover:before,
.viewport_css a:hover:after {
    position: absolute;
    left: 0;
    right: 0;
}
.viewport_css a,
.viewport_css a:hover:after {
    top: 0;
    bottom: 0;
}

.viewport_css a:hover:after {
    content: '';
    display: block;
    z-index: 100;
    background-color: rgba(255, 0, 0, .5);    
}

.viewport_css a:hover:before {
    content: 'View';
    color: #fff;
    top: 50%;
    text-align: center;
    z-index: 200;
    margin-top: -0.5em;
}

.viewport_css .imgwrapper {
    width: 200%;
    height: 200%;
    margin-left: -50%;
    margin-top: -50%;
    transition: all 1s ease-in;  
}

.viewport_css .imgwrapper img {
    width: 100%;
    height: auto;
    display: block;
}

.viewport_css .imgwrapper:hover {
    width: 100%;
    height: 100%;
    margin-left: 0;
    margin-top: 0;
}



回答2:


I altered background-size: 70px 80px; using media queries at each break point - this resolved the issue for me



来源:https://stackoverflow.com/questions/20081908/responsive-image-hover-css-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!