javascript - javascript 的两个元素在 Internet Explorer 中发生冲突

标签 javascript jquery internet-explorer

this page 上有两个元素附加了 Javascript。左侧的捐赠框,中间的图像 slider 。图像 slider 只是在一段时间内滚动,而捐赠框在悬停时更改其左侧位置,然后在释放悬停时返回。这在 FF 和 Chrome 中工作正常。

在 IE 中,这两个元素之间似乎存在某种冲突。一旦我运行悬停命令, slider 就会停止工作。悬停也只能运行一次,并且需要刷新页面才能再次运行。

有什么办法可以解决这个问题吗?

<小时/>

这是图像 slider 的代码:

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

//Set Default State of each portfolio piece
$(".paging").show();
$(".paging a:first").addClass("active");

//Get size of images, how many there are, then determin the size of the image reel.
var imageWidth = $(".window").width();
var imageSum = $(".image_reel img").size();
var imageReelWidth = imageWidth * imageSum;

//Adjust the image reel to its new size
$(".image_reel").css({'width' : imageReelWidth});


//Paging + Slider Function
rotate = function(){    

    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
    var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

    $(".paging a").removeClass('active'); //Remove all active class
    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

    //Slider Animation
    $(".image_reel").animate({ 
        left: -image_reelPosition}, {
            duration: 2000,
            easing: 'easeInOutQuad'
        })      
}



//Rotation + Timing Event
rotateSwitch = function(){      
    play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds

        $active = $('.paging a.active').next();
        if ( $active.length === 0) { //If paging reaches the end...
            $active = $('.paging a:first'); //go back to first
        }

        rotate(); //Trigger the paging and slider function
    }, 6000); //Timer speed in milliseconds (3 seconds)
};

rotateSwitch(); //Run function on launch

//On Hover

$(".image_reel a").hover(function() {
    clearInterval(play); //Stop the rotation
}, function() {
    rotateSwitch(); //Resume rotation
}); 

//On Click
$(".paging a").click(function() {   
    $active = $(this); //Activate the clicked paging
    //Reset Timer
    clearInterval(play); //Stop the rotation
    rotate(); //Trigger rotation immediately
    rotateSwitch(); // Resume rotation
    return false; //Prevent browser jump to link anchor
}); 

});
</script>

这是捐款箱:

$(document).ready(function() {
  $('#donatebox').hover(function() {
    $('#donatebox').animate({
      'left': parseInt($(this).css('left'),10) == 295 ?
        $(this).animate({'left': '0px'}, 1000,'easeOutQuad') :
        295
    });
  });
});
<小时/>

这超出了我的范围,所以我用另一种不需要解析的方式解决了这个问题(我真的不明白......)

$(document).ready(function() {

$('#donatebox').mouseenter(

function() {
$('#donatebox').animate({"marginLeft": -5}, 1000, "easeOutBounce");
});
});

$(document).ready(function() {
$("#donatebox").mouseleave (

function() {
$(this).animate({"marginLeft": -5}, 20).animate({"marginLeft": -305}, 700,     "easeOutQuad"); 

});
});

最佳答案

div 缓回到其插槽中时,某些东西会导致 jQuery“缓动”插件中出现 Javascript 错误。这就是为什么在您使用“捐赠”框一次后没有脚本运行的原因。

如果您在 IE 中使用开发工具,您会发现错误已被捕获。然后(尽管动画是解耦的,因此您不能只向上调用堆栈)可以推断问题出在您从 custom.js 发起的动画中:

$('#donatebox').animate({
  'left': parseInt($(this).css('left'),10) == 295 ?
    $(this).animate({'left': '0px'}, 1000,'easeOutQuad') :
    295
});

^ 我觉得不合适。

您还有重复的 HTML 节点 ID overimage。 ID唯一;也许您打算使用类?

关于javascript - javascript 的两个元素在 Internet Explorer 中发生冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6570849/

相关文章:

javascript - 正则表达式 - 为什么空括号?

http - Windows Phone 在 1 分钟后超时 http 请求

javascript - 在 highcharts 中,图例中的数据在数据小于 10000 的情况下重叠

javascript - 在 Vue.js 单文件组件中使用 jQuery 插件

html - 带有div的ie7 css在TD中右对齐

javascript - IE5 最大宽度支持和可能的解决方案

javascript - 如何在屏幕上显示元素而不是在 html/css 中的屏幕顶部?

javascript - string split and check if parts 以防止错误

javascript - 如何配置 ESLint 使其不允许默认导出

javascript - onclick 没有获得正确的值