IE8 中的Javascript/CSS

标签 javascript jquery internet-explorer-8

我遇到的问题是在 IE 中旋转图像(准确地说是时钟指针)。下面的脚本在一定程度上有效(实际上有动画在进行)但它完全偏离轴旋转。

我绝不是 Javascript/Jquery 的高手,当涉及到如何在 IE8 中正确执行此操作时,我有点不知所措。

代码如下:

(function(jQuery)
{
  jQuery.fn.clock = function(options)
  {
    var defaults = {
      offset: '+0',
      type: 'analog'
    };
    var _this = this;
    var opts = jQuery.extend(defaults, options);

    setInterval( function() {
      var seconds = jQuery.calcTime(opts.offset).getSeconds();
      if(opts.type=='analog')
      {
        var sdegree = seconds * 6;
        var srotate = "rotate(" + sdegree + "deg)";
        var rad = Math.PI/180 * sdegree,
            cos = Math.cos(rad),
            sin = Math.sin(rad);
        jQuery(_this).find(".sec").css({"-moz-transform" : srotate, "-webkit-transform" : srotate, "-ms-transform" : srotate,
                                        'filter': "progid:DXImageTransform.Microsoft.Matrix(M11="+cos+", M12="+(-sin)+", M21="+sin+", M22="+cos+", SizingMethod='auto expand')"});
      }
      else
      {
        jQuery(_this).find(".sec").html(seconds);
      }
    }, 1000 );

    setInterval( function() {
      var hours = jQuery.calcTime(opts.offset).getHours();
      var mins = jQuery.calcTime(opts.offset).getMinutes();
      if(opts.type=='analog')
      {
        var hdegree = hours * 30 + (mins / 2);
        var hrotate = "rotate(" + hdegree + "deg)";
        var rad = Math.PI/180 * hdegree,
            cos = Math.cos(rad),
            sin = Math.sin(rad);
        jQuery(_this).find(".hour").css({"-moz-transform" : hrotate, "-webkit-transform" : hrotate, "-ms-transform" : hrotate,
                                         'filter': "progid:DXImageTransform.Microsoft.Matrix(M11="+cos+", M12="+(-sin)+", M21="+sin+", M22="+cos+", SizingMethod='auto expand')"});
      }
      else
      {
        jQuery(_this).find(".hour").html(hours+':');
      }
      var meridiem = hours<12?'AM':'PM';
      jQuery(_this).find('.meridiem').html(meridiem);
    }, 1000 );

    setInterval( function() {
      var mins = jQuery.calcTime(opts.offset).getMinutes();
      if(opts.type=='analog')
      {
        var mdegree = mins * 6;
        var mrotate = "rotate(" + mdegree + "deg)";
        var rad = Math.PI/180 * mdegree,
            cos = Math.cos(rad),
            sin = Math.sin(rad);
        jQuery(_this).find(".min").css({"-moz-transform" : mrotate, "-webkit-transform" : mrotate, "-ms-transform" : mrotate,
                                        'filter': "progid:DXImageTransform.Microsoft.Matrix(M11="+cos+", M12="+(-sin)+", M21="+sin+", M22="+cos+", SizingMethod='auto expand')"});                
      }
      else
      {
        jQuery(_this).find(".min").html(mins+':');
      }
    }, 1000 );
  }
})(jQuery);


jQuery.calcTime = function(offset) {
  d = new Date();
  utc = d.getTime() + (d.getTimezoneOffset() * 60000);
  nd = new Date(utc + (3600000*offset));
  return nd;
};

最佳答案

检查这个例子: 这是在 IE 上工作...

createLine: function(x1, y1, x2, y2, options){

              // Check if browser is Internet Exploder ;)
              var isIE = navigator.userAgent.indexOf("MSIE") > -1;
              if (x2 < x1){
                var temp = x1;
                x1 = x2;
                x2 = temp;
                temp = y1;
                y1 = y2;
                y2 = temp;
              }
              var line = document.createElement("div");
              line.className = "global_dashboard_line";

              // Formula for the distance between two points
              // http://www.mathopenref.com/coorddist.html
              var length = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

              line.style.width = length + "px";
              line.style.borderColor = options.color;
              line.style.zIndex = options.zindex;
              line.style.borderWidth = options.stroke + " 0px 0px 0px";

              if(isIE){
                line.style.top = (y2 > y1) ? y1 + "px" : y2 + "px";
                line.style.left = x1 + "px";
                var nCos = (x2-x1)/length;
                var nSin = (y2-y1)/length;
                line.style.filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=" + nCos + ", M12=" + -1*nSin + ", M21=" + nSin + ", M22=" + nCos + ")";
              }else{
                var angle = Math.atan((y2-y1)/(x2-x1));
                line.style.top = y1 + 0.5*length*Math.sin(angle) + "px";
                line.style.left = x1 - 0.5*length*(1 - Math.cos(angle)) + "px";
                line.style.MozTransform = line.style.WebkitTransform = line.style.OTransform= "rotate(" + angle + "rad)";
              }
              return line;
            }
    }

这是我为在 html 元素中绘制线条而制作的插件的功能...它对斜线使用旋转。如果你想检查整个插件去: https://github.com/tbem/jquery.line

关于IE8 中的Javascript/CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9911443/

相关文章:

javascript - Jquery - 图像未在第一次点击时加载

jquery - 如何使用 JQuery 删除父元素的子元素的所有事件处理程序

javascript:表单不会仅在 IE 中提交,但是它适用于所有其他浏览器

internet-explorer-8 - wmd 编辑器在 IE8 中中断

javascript - 从 GeoJSON 集合中删除重复值

javascript - 鼠标悬停时暂停打开 div

javascript - JSONP 从 WCF 服务获取缓存响应时出现问题

javascript - 悬停时在 SVG 元素上显示文本

javascript - ie8 var w= window.open() - "Message: Invalid argument."

javascript - 固定顶部导航栏下方的响应式全屏视频