CSSを使用してホバー時に画像にズーム効果を作成しますか?


87

現在、4つの画像の1つにカーソルを合わせるとズーム効果を作成しようとしています。問題は、ほとんどの例で、通常、テーブルまたはマスクdivを使用して何らかの効果を適用することです。

これが私がこれを望むものを実装する1つの例です。

これはこれまでの私のコードです。

HTML

<div id="menu">
<img class ="blog" src="http://s18.postimg.org/il7hbk7i1/image.png" alt="">
<img class ="music" src="http://s18.postimg.org/4st2fxgqh/image.png" alt="">
<img class ="projects" src="http://s18.postimg.org/sxtrxn115/image.png" alt="">
<img class ="bio" src="http://s18.postimg.org/5xn4lb37d/image.png" alt="">
</div>

CSS

#menu {
    max-width: 1200px;
    text-align: center;
    margin: auto;
}

#menu img {
    width: 250px;
    height: 375px;
    padding: 0px 5px 0px 5px;
    overflow: hidden;
}

.blog {
    height: 375px;
    -webkit-transition: all 1s ease;
    -moz-transition: all 1s ease;
    -o-transition: all 1s ease;
    -ms-transition: all 1s ease;
    transition: all 1s ease;
}

.blog:hover {
    cursor: pointer;
    height:475px;
    width: 350px;
}

.music {
    height: 375px;
}

.projects {
    height: 375px;
}

.bio {
    height: 375px;
}

回答:


164

CSS3 transformプロパティを使用scaleして、ズームのような効果を与えるものを使用するのはどうですか、これはそのように行うことができます、

HTML

<div class="thumbnail">
    <div class="image">
        <img  src="http://placehold.it/320x240" alt="Some awesome text"/>
    </div>
</div>

CSS

.thumbnail {
    width: 320px;
    height: 240px;
}

.image {
    width: 100%;
    height: 100%;    
}

.image img {
    -webkit-transition: all 1s ease; /* Safari and Chrome */
    -moz-transition: all 1s ease; /* Firefox */
    -ms-transition: all 1s ease; /* IE 9 */
    -o-transition: all 1s ease; /* Opera */
    transition: all 1s ease;
}

.image:hover img {
    -webkit-transform:scale(1.25); /* Safari and Chrome */
    -moz-transform:scale(1.25); /* Firefox */
    -ms-transform:scale(1.25); /* IE 9 */
    -o-transform:scale(1.25); /* Opera */
     transform:scale(1.25);
}

これがデモフィドルです。簡単にするために要素の一部を削除しました。いつでもオーバーフローを非表示に追加して.image、拡大縮小された画像のオーバーフローを非表示にすることができます。

zoom プロパティはIEでのみ機能します


14
そして、「コードからの深い人生の教訓」賞を受賞したあなたのCSSは、「すべてが簡単、すべてが簡単」と唱えています。
nerrolken 2014年

1
ピクセル化をどのように防ぎますか?
ワルタリ2016

1
@Waltari画像サイズはコンテンツ領域より25%(ズームインする量)大きくする必要があるため、サムネイルが320x240で、25%ズームインする場合、画像は400x300になります。
ミッチェルレイゼル2017

25

どうぞ。

デモ

http://jsfiddle.net/27Syr/4/

HTML

<div id="menu">

    <div class="fader">
        <div class="text">
            <p>Yay!</p>
        </div>
        <a href="http://stackoverflow.com/questions/15732643/jquery-masonry-and-css3/">
            <img class ="blog" src="http://s18.postimg.org/il7hbk7i1/image.png" alt="">
        </a>
    </div>

    <div class="fader">
        <div class="text">
            <p>Yay!</p>
        </div>
        <a href="http://stackoverflow.com/questions/15732643/jquery-masonry-and-css3/">
            <img class ="blog" src="http://s18.postimg.org/4st2fxgqh/image.png" alt="">
        </a>
    </div>

    <div class="fader">
        <div class="text">
            <p>Yay!</p>
        </div>
        <a href="http://stackoverflow.com/questions/15732643/jquery-masonry-and-css3/">
            <img class ="projects" src="http://s18.postimg.org/sxtrxn115/image.png" alt="">
        </a>
    </div>

    <div class="fader">
        <div class="text">
            <p>Yay!</p>
        </div>
        <a href="http://stackoverflow.com/questions/15732643/jquery-masonry-and-css3/">
            <img class ="blog" src="http://s18.postimg.org/5xn4lb37d/image.png" alt="">
        </a>
    </div>

</div>

CSS

#menu {
    text-align: center; }


.fader {
    /* Giving equal sizes to each element */
    width:    250px;
    height:   375px;

    /* Positioning elements in lines */
    display:  inline-block;

    /* This is necessary for position:absolute to work as desired */
    position: relative;

    /* Preventing zoomed images to grow outside their elements */
    overflow: hidden; }


    .fader img {
        /* Stretching the images to fill whole elements */
        width:       100%;
        height:      100%;

        /* Preventing blank space below the image */
        line-height: 0;

        /* A one-second transition was to disturbing for me */
        -webkit-transition: all 0.3s ease;
        -moz-transition:    all 0.3s ease;
        -o-transition:      all 0.3s ease;
        -ms-transition:     all 0.3s ease;
        transition:         all 0.3s ease; }

        .fader img:hover {
            /* Making images appear bigger and transparent on mouseover */
            opacity: 0.5;
            width:   120%;
            height:  120%; }

    .fader .text {
        /* Placing text behind images */
        z-index: -10;     

        /* Positioning text top-left conrner in the middle of elements */
        position: absolute;
        top:      50%;
        left:     50%; }

        .fader .text p {
            /* Positioning text contents 50% left and top relative
               to text container's top left corner */
            margin-top:  -50%; 
            margin-left: -50%; }

提案

.fader { inline-block; }グリッドシステムの使用を検討する代わりに。好みのあなたの技術をベースに、あなたが行くことができ財団スージー石造またはその選択肢を。


19
.aku { 
    transition: all .2s ease-in-out; 
}

.aku:hover {
    transform: scale(1.1); 
}

16

背景画像を使うのが好きです。私はそれがより簡単でより柔軟だと思います:

デモ

CSS:

#menu {
    max-width: 1200px;
    text-align: center;
    margin: auto;
}
.zoomimg {
    display: inline-block;
    width: 250px;
    height: 375px;
    padding: 0px 5px 0px 5px;
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-position: center center;
    transition: all .5s ease;
}
.zoomimg:hover {
    cursor: pointer;
    background-size: 150% 150%;
}
.blog {
    background-image: url(http://s18.postimg.org/il7hbk7i1/image.png);
}
.music {
    background-image: url(http://s18.postimg.org/4st2fxgqh/image.png);
}
.projects {
    background-image: url(http://s18.postimg.org/sxtrxn115/image.png);
}
.bio {
    background-image: url(http://s18.postimg.org/5xn4lb37d/image.png);
}

HTML:

<div id="menu">
    <div class="blog zoomimg"></div>
    <div class="music zoomimg"></div>
    <div class="projects zoomimg"></div>
    <div class="bio zoomimg"></div>
</div>

オーバーレイ付きデモ2


これに背景画像を使用することはお勧めしません。画像タグとalt属性の使用に伴う素晴らしい画像関連のSEOをすべて失うことになります。
ミッチェルレイゼル2014

これは素晴らしいことだと思いますが、imgタグの有用性はすべて失われます。
ダニエルナーブ2014

画像がSEOに関連していない場合は、このソリューションがよりクリーンであることがわかりました。
nLemay 2016

7

単に:

.grow { transition: all .2s ease-in-out; }

これにより、要素はcssを介してアニメーションを割り当てることができます。

.grow:hover { transform: scale(1.1); }

これはそれを成長させるでしょう!



3

解決策1:ズームマスターをダウンロードできます。
解決策2:ここに 移動します
解決策3: 独自のコード

.hover-zoom {
    -moz-transition:all 0.3s;
    -webkit-transition:all 0.3s;
     transition:all 0.3s
 }
.hover-zoom:hover {
    -moz-transform: scale(1.1);
    -webkit-transform: scale(1.1);
     transform: scale(1.5)
 }
<img class="hover-zoom"  src="https://i.stack.imgur.com/ewRqh.jpg" 
     width="100px"/>



0
 -webkit-transition: all 1s ease; /* Safari and Chrome */
-moz-transition: all 1s ease; /* Firefox */
-ms-transition: all 1s ease; /* IE 9 */
-o-transition: all 1s ease; /* Opera */
transition: all 1s ease;  

上記の遷移についてメモしたいだけです

 -webkit-transition: all 1s ease; /* Safari and Chrome */
transition: all 1s ease;

そして-ms-は確かにIE9で動作しますが、どこからそのアイデアを得たのかわかりません。


IE10(移行をサポートするIEの最初の安定バージョン)は-ms-プレフィックスを必要としません。以前の不安定なビルドは必要であり、同じことが-moz-と-o-にも当てはまります。現在のバージョンは必要ありませんが、以前のビルドは必要です。
ミッチェルレイゼル2015

-ms-は、移行を機能させるためのIEブラウザーのソリューションを提供しないため、完全に必要ありません。プレフィックスを使用して特定のルールを機能させるために、単に楽しみのためにそれらを投入するのではありません。
DCdaz 2015年

それがに来るとき-ms-はIE 9のように、以前のバージョンはまだベンダー接頭辞を使用する意味、彼らはIE 10のリリースに落下することをベンダー接頭辞で、多くの人々は、悲しいことに、今、この日にIE 9を使用してtransitionIEが正式にもたらした財産transitionにIE 10なので、それは必要ありませんが、100%になりたい場合は、を使用する不安定なバージョンのIE9があります-ms-transition
ミッチェルレイゼル2015年

1
これは誤った遷移であり、ベンダープレフィックスが付いたIE9では機能しません。-ms-は、トランジションの完全に無意味なプレフィックスです。
DCdaz 2015年

1
オペラモバイルの-ms-もどこでも-o-は実際にはトランジションを機能させないので、2011年のリリースが連続するのは無意味です。文字通りほんの一握りのユーザーについて話しているので無意味です。 100%は、一般ユーザーとしてではなくプラットフォームの目的でWebを使用する開発者および機関です。しかし、それでも主な議論は-ms-のままであり、まったく役に立たず、少しも必要ありません。-moz-または-o-は必要ありません。また、-ms-も100%必要ありません。– DCdaz 2015
13:07

0

    .img-wrap:hover img {
        transform: scale(0.8);
    }
    .img-wrap img {
        display: block;
        transition: all 0.3s ease 0s;
        width: 100%;
    }
    <div class="img-wrap">
    <img src="http://www.sampleimages/images.jpg"/> // Your image
    </div>

このコードはズームアウト効果専用です。スタイルに応じてdiv "img-wrap"を設定し、上記のスタイル結果のズームアウト効果を挿入します。ズームイン効果の場合は、スケール値を大きくする必要があります(例:ズームの場合-で、変換を使用します:scale(1.3);


0
  <div class="item">
  <img src="yamahdi1.jpg" alt="pepsi" width="50" height="58">
  <img src="yamahdi.jpg" alt="pepsi" width="50" height="58">
  <div class="item-overlay top"></div>

css:

.item img {

  -moz-transition: all 0.3s;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}
.item img:hover {
  -moz-transform: scale(1.1);
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

0

@import url('https://fonts.googleapis.com/css?family=Muli:200,300,400,700&subset=latin-ext');
 body{ font-family: 'Muli', sans-serif; color:white;}
 #lists {
   width: 350px;
   height: 460px;
   overflow: hidden;
   background-color:#222222;
   padding:0px;
   float:left;
    margin: 10px;
 }
 
 .listimg {
   width: 100%;
   height: 220px;
   overflow: hidden;
   float: left;
  
 }
  #lists .listimg img {
   width: 350px;
   height: 220px;
   -moz-transition: all 0.3s;
   -webkit-transition: all 0.3s;
   transition: all 0.3s;
 }
 #lists:hover{cursor: pointer;}
 #lists:hover > .listimg img {
   -moz-transform: scale(1.3);
   -webkit-transform: scale(1.3);
   transform: scale(1.3);
   -webkit-filter: blur(5px);
   filter: blur(5px);
 }

#lists h1{margin:20px; display:inline-block; margin-bottom:0px; }
#lists p{margin:20px;}

.listdetail{ text-align:right; font-weight:200; padding-top:6px;padding-bottom:6px;}
<div id="lists">
<div class="listimg">
  <img src="https://lh3.googleusercontent.com/WeEw5I-wk2UO-y0u3Wsv8MxprCJjxTyTzvwdEc9pcdTsZVj_yK5thdtXNDKoZcUOHlegFhx7=w1920-h914-rw">
</div>
<div class="listtext">
<h1>Eyes Lorem Impsum Samet</h1>
<p>Impsum Samet Lorem</p>
</div>
<div class="listdetail">
<p>Click for More Details...</p>
</div>
</div>

<div id="lists">
<div class="listimg">
  <img src="https://lh4.googleusercontent.com/fqK7aQ7auobK_NyXRYCsL9SOpVj6SoYqVlgbOENw6IqQvEWzym_3988798NlkGDzu0MWnR-7nxIhj7g=w1920-h870-rw">
</div>
<div class="listtext">
<h1>Two Frogs Lorem Impsum Samet</h1>
<p>Impsum Samet Lorem</p>
</div>
<div class="listdetail">
<p>More Details...</p>
</div>
</div>


0
<!DOCTYPE html>
<html>
<head>
<style>
.zoom {

  overflow: hidden;
}

.zoom img {
  transition: transform .5s ease;
}
.zoom:hover img {
  transform: scale(1.5);
}

</style>
</head>
<body>

<h1>Image Zoom On Hover</h1>
<div class="zoom">
  <img src="/image-path-url" alt="">
</div>

</body>
</html>

低品質の投稿からあなたの答えを削除するためにいくつかの説明を追加します。
HarshaBiyani19年

-3

jQueryJavaScriptライブラリをjquery.zbox.cssおよびjquery.zbox.jsと一緒にWebページに追加します。

<link rel="stylesheet" href="jquery.zbox.css">
<script src="jquery.min.js"></script>
<script src="jquery.zbox.min.js"></script>

フルサイズの画像を指すリンクを含むサムネイルのグループをWebページに追加します。

<a class="zb" rel="group" href="1.png" title="Image 1">
  <img src="thumb1.png">
</a>
<a class="zb" rel="group" href="2.png" title="Image 2">
  <img src="thumb2.png">
</a>
<a class="zb" rel="group" href="3.png" title="Image 3">
 <img src="thumb3.png">
</a>

ドキュメントレディで関数を呼び出します。それでおしまい。

ビューソースで次のことを行います。

$(".zb").zbox();

2
OPはCSSでこれを行う方法を求めました。jQueryの回答はここでは役に立ちません。
TylerH 2016年
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.