简体   繁体   English

如何在鼠标悬停循环中更改图像大小?

[英]How to Changing image size during mouse hover in loop?

When I apply Javascript on Below code it effect on only first image only first image size is change even my mouse is hovering over any image 当我在下面的代码上应用Javascript时,它仅在第一张图像上起作用,即使我的鼠标悬停在任何图像上,第一张图像的大小也不会改变

<div class="container">
    <div class="row">
        @foreach( $gigs as $gig )
        <div class=" col-lg-3 col-md-3 col-sm-6 col-xs-12 gigbox">
            <div class="panel panel-default" style="height: 285px">
                <img src="{{URL::asset($gig->image)}}" id="img" width="100%" onmouseover="changesize()" height="160"  alt="">
                <ul class="skillUl">
                    <li class="gigtitle" style="list-style: none"><a  href="{{route('gig',['gig_id'=>$gig->id, 'gig_title'=>$gig->gigtitle])}}"> I will  {{$gig->gigtitle}}</a></li>
                    <a class="startingFrom" href="{{route('gig',['gig_id'=>$gig->id, 'gig_title'=>$gig->gigtitle])}}">Starting From <Span class="price"> Rs {{$gig->price}}</Span></a>
                </ul>
            </div>
        </div>
        @endforeach
    </div>
</div>

javaScript Code is here javaScript代码在这里

<script>
    function changesize() {
        document.getElementById("img").style.width = "130px";
    }
</script>

css sample CSS样本

img { transition: all .2s ease-in-out; }
img:hover { transform: scale(1.1); }

You are using a loop to create multiple elements with id="img" . 您正在使用循环创建带有id="img"多个元素。 id s are supposed to be unique on any given page. id在任何给定页面上都应该是唯一的。 You should instead use class="img" and use the getElementsByClassName() method: 您应该改用class="img"并使用getElementsByClassName()方法:

document.getElementsByClassName("img").style.width = "130px";

You also should consider Frank Wisniewski's suggestion to use CSS. 您还应该考虑Frank Wisniewski提出的使用CSS的建议。 You can probably achieve most or all of what you've described without javascript. 您可能不需要JavaScript就可以实现您描述的大部分或全部内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM