简体   繁体   English

单击箭头图像时仅移动图像

[英]Move only image when arrow image is clicked

I have 7 images when click the left arrow i want to move the images towards left also same as right but my problem is when i click left and right arrow image also move along with image can any tell me to how move only the images not an arrow image see my code below 我有7张图片,当我单击向左箭头时我想向左也向右移动图片,但是我的问题是当我单击向左和向右箭头时,图片也随图片一起移动,可以告诉我如何仅将图片移动而不是箭头图像,请参阅下面的代码

Html HTML

<input id="moveleft" type="image" style="margin:13px 586px 6px -683px" src="image/left.png" >
<input id="moveright" type="image" style="margin:51px 0 0 62px" src="image/right.png" >



    <div class="img" id="textbox">

 <img  src="image/welcome.png" alt="welcome" width="87" height="137" style="margin:3px 0 0 -2px">

 <img src="image/happynewyear.jpg" alt="happynewyear" width="92" height="131" style="margin:-5px 0 5px -5px">

 <img src="image/happyeaster.jpg" alt="easter" width="92" height="131" style="margin:-1px 0 5px">

  <img src="image/imarahton.jpg" alt="easter" width="92" height="131" style="margin:-1px 0 4px -3px">

  <img src="image/happybirthday.jpg" alt="easter" width="93" height="131" style="margin:-4px 0 4px -3px">

   <img src="image/summer.jpg" alt="easter" width="93" height="131" style="margin:-4px 0 4px -2px">

    <img src="image/valentine.jpg" alt="easter" width="91" height="131" style="margin:-1px 0 4px -2px">

</div> 

Javascript Java脚本

<script type="text/javascript">
$(document).ready(function() {

    $('#moveleft').click(function() {
        $('#textbox').animate({
        'marginLeft' : "-=30px" //moves left
        });
    });

    $('#moveright').click(function() {
        $('#textbox').animate({
        'marginLeft' : "+=30px" //moves right
        });
    });



});
</script>

Use position: absolute for both arrows and position: relative to its container. 使用position: absolute用于箭头,以及position: relative于其容器。 Sett appropriate position for the arrows using top / right / bottom / left properties. 使用top / right / bottom / left属性为箭头设置适当的位置。

EDIT: 编辑:

 $(document).ready(function() { $('#moveleft').click(function() { $('#textbox').animate({ 'marginLeft' : "-=30px" //moves left }); }); $('#moveright').click(function() { $('#textbox').animate({ 'marginLeft' : "+=30px" //moves right }); }); }); 
 input{ display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="moveleft" type="image" src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-left-128.png" > <input id="moveright" type="image" src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-right-128.png" > <div class="img" id="textbox"> <img src="image/welcome.png" alt="welcome" width="87" height="137" style="margin:3px 0 0 -2px"> <img src="image/happynewyear.jpg" alt="happynewyear" width="92" height="131" style="margin:-5px 0 5px -5px"> <img src="image/happyeaster.jpg" alt="easter" width="92" height="131" style="margin:-1px 0 5px"> <img src="image/imarahton.jpg" alt="easter" width="92" height="131" style="margin:-1px 0 4px -3px"> <img src="image/happybirthday.jpg" alt="easter" width="93" height="131" style="margin:-4px 0 4px -3px"> <img src="image/summer.jpg" alt="easter" width="93" height="131" style="margin:-4px 0 4px -2px"> <img src="image/valentine.jpg" alt="easter" width="91" height="131" style="margin:-1px 0 4px -2px"> </div> 

Change you HTMl, and CSS like example below. 更改您的HTMl和CSS,如下例所示。

HTML: HTML:

<div class="slider">
    <input id="moveleft" type="image" src="image/left.png">
    <input id="moveright" type="image" src="image/right.png">



    <div class="img" id="textbox">

        <img src="image/welcome.png" alt="welcome" width="87" height="137" style="margin:3px 0 0 -2px">

        <img src="image/happynewyear.jpg" alt="happynewyear" width="92" height="131" style="margin:-5px 0 5px -5px">

        <img src="image/happyeaster.jpg" alt="easter" width="92" height="131" style="margin:-1px 0 5px">

        <img src="image/imarahton.jpg" alt="easter" width="92" height="131" style="margin:-1px 0 4px -3px">

        <img src="image/happybirthday.jpg" alt="easter" width="93" height="131" style="margin:-4px 0 4px -3px">

        <img src="image/summer.jpg" alt="easter" width="93" height="131" style="margin:-4px 0 4px -2px">

        <img src="image/valentine.jpg" alt="easter" width="91" height="131" style="margin:-1px 0 4px -2px">

    </div> 

CSS: CSS:

.slider {
        position:relative;
    }
    #moveleft, #moveright {
        position:absolute;
        width:16px;
        height:16px;
        top:50%;
        margin-top:-8px;
    }
    #moveleft {
        left:5px;
    }
     #moveright {
        right:5px;
    }

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

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