简体   繁体   English

图片旋转360度

[英]image rotate 360 degree on click

Need your help developers, I am using images as a menu. 需要开发人员的帮助,我正在使用图像作为菜单。 I just want when i click on image it rotate 360 degree and then another page is open. 我只想在单击图像时将其旋转360度,然后打开另一个页面。 i try this. 我尝试这个。

<style>
    .image {
        overflow: hidden;
        transition-duration: 0.8s;
        transition-property: transform;
    }
    .image:active {
        -webkit-transform: rotate(360deg);
    }
</style>

html: HTML:

<img class="image" src="img path">

in this code image rotation is depend on click time and i want user just click once image rotate 360 degree and the link page display. 在此代码中,图像旋转取决于单击时间,我希望用户只需单击一次图像即可旋转360度并显示链接页面。 but this is not i want. 但这不是我想要的。 I am using jqueryMobile and phonegap 我正在使用jqueryMobile和phonegap

thanks in advance. 提前致谢。

You can put the link url in the image as a data attribute: 您可以将链接网址作为数据属性放在图片中:

<img id="theimage" data-linkurl="#page2"src="http://makeameme.org/media/templates/120/grumpy_cat.jpg" alt=""  />

Then when you handle the click event, 然后,当您处理click事件时,

You add the animation class. 您添加动画类。

You add an animationEnd handler that fires when the animation is complete. 您添加了animationEnd处理程序,该处理程序在动画完成时触发。 Use one() instead of on() as you only want this handler to fire once. 使用one()代替on(),因为您只希望此处理程序触发一次。

In the animationEnd handler you remove the animation class (so you can add it again next time), get the url from the data-attribute, and then navigate to the page. 在animationEnd处理程序中,删除动画类(以便您下次可以再次添加它),从数据属性获取url,然后导航到页面。

$("#theimage").on("click", function(){       
    $(this).addClass("imageRot").one('webkitAnimationEnd mozAnimationEnd oAnimationEnd msAnimationEnd animationend', function () {
        $(this).removeClass("imageRot"); //remove anim class
        var url = $(this).data('linkurl'); //get url from data-attribute
        $( ":mobile-pagecontainer" ).pagecontainer( "change", url); //navigate to page      
    });
});

For the animation class I have used @cracker's spin animation (thanks cracker!): 对于动画类,我使用了@cracker的旋转动画(感谢cracker!):

.imageRot {
   -webkit-animation:spin 2s ease-in-out;
    -moz-animation:spin 2s ease-in-out;
    animation:spin 2s ease-in-out;
}
@-moz-keyframes spin {
    100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); }
}
@-webkit-keyframes spin {
    100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); }
}
@keyframes spin { 
    100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } 
}

Here is a working DEMO 这是一个有效的演示

try it: 试试吧:

<style>
  .image {
    overflow: hidden;
    -webkit-transition: transform 0.8s;
    transition: transform 0.8s;
  }

  .image:active {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
</style>
  1. You didn't include a webkit option ( -webkit-* ) in transition . 您没有在transition包含一个webkit选项( -webkit-* )。

  2. You didn't include a non-webkit option in transform . 您没有在transform包括非webkit选项。

because of that, no matter what browser you were using, something were missing ( transform or transition ), and therefore the code didn't work on any browser. 因此,无论您使用哪种浏览器,都缺少某些内容( transformtransition ),因此该代码在任何浏览器上均不起作用。

edit : I noticed it wasn't what you were asking for. 编辑 :我注意到这不是您想要的。 I don't believe that it can be done with CSS only. 我不认为只能使用CSS做到这一点。 If you want, you can do it with jQuery: 如果需要,可以使用jQuery:

<script>
  $(".image").click(function(){
    $(this).addClass("clicked").delay(800).removeClass("clicked");
  });
</script>

<style>
  .image {
    overflow: hidden;
    -webkit-transition: transform 0.8s;
    transition: transform 0.8s;
  }

  .image.clicked {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
</style>

you need to try using 您需要尝试使用

.image {
   -webkit-animation:spin 4s ease-in-out; // No more infinite
    -moz-animation:spin 4s linear;
    animation:spin 4s linear;
}


@-moz-keyframes spin {
    100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); }
}
@-webkit-keyframes spin {
    100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); }
}
@keyframes spin { 
    100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } 

OR 要么

@-webkit-keyframes rotate {  
  100% { -webkit-transform: rotate(360deg); }
}

.rotate {
    -webkit-animation-name: rotate; 
    -webkit-animation-duration: 4.5s; 
    -webkit-animation-iteration-count: infinite;
    -webkit-transition-timing-function: linear;
}

DEMO1 demo1的

DEMO2 DEMO2

HTML HTML

<img src = "some_image.png" alt = "test" class = "rotative" />

CSS CSS

.canRotate
{
    -webkit-animation: FullRotation 3s ease-out;
    -o-animation: FullRotation 3s ease-out;
    -ms-animation: FullRotation 3s ease-out;
    -moz-animation: FullRotation 3s ease-out;
    animation: FullRotation 3s ease-out;
} 

@-webkit-keyframes FullRotation
{
    from { -webkit-transform: rotate(0deg); }
    to { -webkit-transform: rotate(360deg); }
}

@-o-keyframes FullRotation
{
    from { -o-transform: rotate(0deg); }
    to { -o-transform: rotate(360deg); }
}

@-ms-keyframes FullRotation
{
    from { -ms-transform: rotate(0deg); }
    to { -ms-transform: rotate(360deg); }
}

@-moz-keyframes FullRotation
{
    from { -moz-transform: rotate(0deg); }
    to { -moz-transform: rotate(360deg); }
}

@keyframes FullRotation
{
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

JavaScript JavaScript的

function RotateOnClickAndOpenPage(classname, url)
{
    var elts = document.getElementsByClassName(classname);

    for(var i = 0; i < elts.length; ++i)
    {
        elts[i].onclick = function(){
            this.style.className = "canRotate";
            var that = this;
            setTimeout(function(){
                window.open(url);
                that.style.className = "cannotRotate";
            }, 3000);
        };
    }
}

// Exemple
RotateOnClickAndOpenPage("rotative", "http://www.google.fr");

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

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