Flipping an image with JS/Jquery

前端 未结 4 1067
春和景丽
春和景丽 2020-12-19 18:24

I am looking to flip an image. I have gotten the css to work using:

    -moz-transform: scaleX(-1);
    -o-transform: scaleX(-1);
    -webkit-transform: sca         


        
相关标签:
4条回答
  • 2020-12-19 18:50

    I'm not completely sure I understand what you're looking for.

    I'm thinking perhaps it can be done without any JavaScript at all? If you're looking to flip along the X axis, with some animation?

    Flipping Image on Hover

    JSFiddle: Image Flip on :hover

    For this demo, I had to place the image HTML into a wrapper <div>, because otherwise the :hover and the scale() changes conflict with one another in funky ways. You'll understand if you remove the wrapper <div>.

    HTML

    <div class="flippy">
        <img src="http://lorempixel.com/200/200/"/>
    </div>
    

    CSS:

    .flippy>img {
        /**/-moz-transform:scale(1,1);-webkit-transform:scale(1,1);
        transform:scale(1,1);
        /**/-webkit-transition:all 600ms ease;-webkit-transition:all 600ms ease;
        transition:all 600ms ease; }
    
        .flippy:hover>img {
            /**/-moz-transform:scale(-1,1);-webkit-transform:scale(-1,1);
            transform:scale(-1,1); }
    

    If you need to control it with JavaScript, it should be easy enough to replace the :hover with another class, like .flipped, then do as you please in JS to activate it's flip state on and off.

    //Chase.

    Flipping Image on Attribute (click-based demo)

    jsFiddle: Image Flip on Attribute

    In this demo, the image flips when is has the flipped attribute set.

    JavaScript:

    // Toggles the 'flipped' attribute on the <img> tag.
    $('.flippy').click(function(){
        if ($(this).attr('flipped'))
            $(this).removeAttr('flipped');
        else $(this).attr('flipped','flipped');
    });
    

    CSS:

    /* vendor-prefixes have been removed in this example */
    /* We just change the scale based on the flipped attribute */
    .flippy {
        transform:scale(1,1);
        transition:all 600ms ease; }
    
        .flippy[flipped] {
            transform:scale(-1,1); }
    

    HTML: <img class="flippy" src="http://lorempixel.com/200/200/"/> -- as you can see, we no longer need the <div> wrapper for this example, as the :hover conflicts are no longer an issue.

    //Chase.

    0 讨论(0)
  • 2020-12-19 18:53

    I'd just use a class, like so:

    .flipped {
        -moz-transform: scaleX(-1);
        -o-transform: scaleX(-1);
        -webkit-transform: scaleX(-1);
        transform: scaleX(-1);
        filter: FlipH;
        -ms-filter: "FlipH";
    }
    

    Then just swap the class:

    $("#chicken").delay(2000).fadeOut(1, function() {
        $(this).addClass('flipped').show()
               .animate({ left: 1600 + "px" , top : 2370 + "px"}, 5000, 'linear');
    });
    

    FIDDLE

    0 讨论(0)
  • 2020-12-19 19:02
    <style type="text/css">
        .transform-image {     
            -moz-transform: scaleX(-1);
                -o-transform: scaleX(-1);
                -webkit-transform: scaleX(-1);
                transform: scaleX(-1);
                filter: FlipH;
                -ms-filter: "FlipH";
            }
    </style>
    <script type="text/javascript">
      $("#chicken").hover(function(){
               $(this).addClass("transform-image") },   
         function () {
               $(this).removeClass("transform-image");
             };
    })
    </script>
    
    0 讨论(0)
  • 2020-12-19 19:13

    Are you trying do to something like this?

    $('#image').mouseover(function(){
        $(this).addClass('flipped');
    }).mouseleave(function(){
        $(this).removeClass('flipped');
    });
    

    the css:

    .flipped {
        transform: scale(-1, 1);
        -moz-transform: scale(-1, 1);
        -webkit-transform: scale(-1, 1);
        -o-transform: scale(-1, 1);
        -khtml-transform: scale(-1, 1);
        -ms-transform: scale(-1, 1);
    }
    

    jsFiddle here

    0 讨论(0)
提交回复
热议问题