Jquery 从iPad中排除Javascript函数
我目前正在使用一个脚本,当鼠标移到图标上时会执行淡入淡出效果(它会将图标从颜色淡入黑白)。由于悬停事件,我在iPad或iPhone上无法正常工作 有没有办法将以下内容包装成一个条件,将iPad、iPhone甚至droid设备排除在外Jquery 从iPad中排除Javascript函数,jquery,Jquery,我目前正在使用一个脚本,当鼠标移到图标上时会执行淡入淡出效果(它会将图标从颜色淡入黑白)。由于悬停事件,我在iPad或iPhone上无法正常工作 有没有办法将以下内容包装成一个条件,将iPad、iPhone甚至droid设备排除在外 <script> $(document).ready(function() { $("ul.gallery li").hover(function() { //On hover...
<script>
$(document).ready(function() {
$("ul.gallery li").hover(function() { //On hover...
$(this).siblings().each(function () {
var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
//Set a background image(thumbOver) on the <a> tag
$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
//Fade the image to 0
$(this).find("span").stop(false,false).fadeTo('normal', 0 , function() {
$(this).hide() //Hide the image after fade
});
});
} ,
function() { //on hover out...
$(this).siblings().each(function () {
//Fade the image to 1
$(this).find("span").stop(false,false).fadeTo('normal', 1).show();
});
});
});
</script>
$(文档).ready(函数(){
$($ul.gallery li”).hover(函数(){//On hover。。。
$(this).sides().each(函数(){
var thumbOver=$(this).find(“img”).attr(“src”);//获取图像url并将其分配给“thumbOver”
//在a标签上设置背景图像(拇指)
$(this.find(“a.thumb”).css({'background':'url('+thumbOver+')不重复中间底部'});
//将图像淡入0
$(this).find(“span”).stop(false,false).fadeTo('normal',0,function()){
$(this).hide()//淡入淡出后隐藏图像
});
});
} ,
函数(){//悬停在外时。。。
$(this).sides().each(函数(){
//将图像淡入1
$(this).find(“span”).stop(false,false).fadeTo('normal',1).show();
});
});
});
谢谢您可以使用
navigator.platform检查“iPad”:
// Is the user on an iPad?
var isIpad = navigator.platform.toLowerCase() === "ipad";
类似地,您可以使用对象文字和
中的运算符检查其他idevice:
// Is the user on an iDevice?
var isIDevice = navigator.platform.toLowerCase() in {
"ipod": true,
"ipad": true,
"iphone": true
};
为了防止代码在这些条件下运行,您可以将上面的设置逻辑保留在命名函数中,并在DOM就绪时有条件地执行它,例如:
<script type="text/javascript">
(function() {
// Setup routine
var ready = function() {
$("ul.gallery li").hover(function() { //On hover...
$(this).siblings().each(function () {
var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
//Set a background image(thumbOver) on the <a> tag
$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
//Fade the image to 0
$(this).find("span").stop(false,false).fadeTo('normal', 0 , function() {
$(this).hide() //Hide the image after fade
});
});
},
function() { //on hover out...
$(this).siblings().each(function () {
//Fade the image to 1
$(this).find("span").stop(false,false).fadeTo('normal', 1).show();
});
});
};
// Current platform
var platform = navigator.platform.toLowerCase();
// List of iDevice platforms
var iDevices = {
"ipod": true,
"ipad": true,
"iphone": true
};
/*
// OPTION 1:
// On DOM-ready, execute for everthing except iPad
$(function() {
if ( platform !== "ipad" ) {
ready();
}
});
*/
// OPTION 2
// Only execute if not an iDevice
$(function() {
if ( !(platform in iDevices) ) {
ready();
$(window).resize(function () {
viewportSize();
});
}
});
})();
(功能(){
//设置程序
var ready=function(){
$($ul.gallery li”).hover(函数(){//On hover。。。
$(this).sides().each(函数(){
var thumbOver=$(this).find(“img”).attr(“src”);//获取图像url并将其分配给“thumbOver”
//在a标签上设置背景图像(拇指)
$(this.find(“a.thumb”).css({'background':'url('+thumbOver+')不重复中间底部'});
//将图像淡入0
$(this).find(“span”).stop(false,false).fadeTo('normal',0,function()){
$(this).hide()//淡入淡出后隐藏图像
});
});
},
函数(){//悬停在外时。。。
$(this).sides().each(函数(){
//将图像淡入1
$(this).find(“span”).stop(false,false).fadeTo('normal',1).show();
});
});
};
//当前平台
var platform=navigator.platform.toLowerCase();
//iDevice平台列表
变量iDevices={
“ipod”:没错,
“ipad”:没错,
“iphone”:真的吗
};
/*
//备选案文1:
//在DOM就绪状态下,执行除iPad之外的所有操作
$(函数(){
如果(平台!=“ipad”){
ready();
}
});
*/
//选择2
//仅当不是iDevice时执行
$(函数(){
if(!(iDevices中的平台)){
ready();
$(窗口)。调整大小(函数(){
视口大小();
});
}
});
})();
老实说,我从未做过android检测,但应该有类似的方法
希望有帮助!干杯。您可以使用navigator.platform检查“iPad”:
// Is the user on an iPad?
var isIpad = navigator.platform.toLowerCase() === "ipad";
类似地,您可以使用对象文字和
中的运算符检查其他idevice:
// Is the user on an iDevice?
var isIDevice = navigator.platform.toLowerCase() in {
"ipod": true,
"ipad": true,
"iphone": true
};
为了防止代码在这些条件下运行,您可以将上面的设置逻辑保留在命名函数中,并在DOM就绪时有条件地执行它,例如:
<script type="text/javascript">
(function() {
// Setup routine
var ready = function() {
$("ul.gallery li").hover(function() { //On hover...
$(this).siblings().each(function () {
var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
//Set a background image(thumbOver) on the <a> tag
$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
//Fade the image to 0
$(this).find("span").stop(false,false).fadeTo('normal', 0 , function() {
$(this).hide() //Hide the image after fade
});
});
},
function() { //on hover out...
$(this).siblings().each(function () {
//Fade the image to 1
$(this).find("span").stop(false,false).fadeTo('normal', 1).show();
});
});
};
// Current platform
var platform = navigator.platform.toLowerCase();
// List of iDevice platforms
var iDevices = {
"ipod": true,
"ipad": true,
"iphone": true
};
/*
// OPTION 1:
// On DOM-ready, execute for everthing except iPad
$(function() {
if ( platform !== "ipad" ) {
ready();
}
});
*/
// OPTION 2
// Only execute if not an iDevice
$(function() {
if ( !(platform in iDevices) ) {
ready();
$(window).resize(function () {
viewportSize();
});
}
});
})();
(功能(){
//设置程序
var ready=function(){
$($ul.gallery li”).hover(函数(){//On hover。。。
$(this).sides().each(函数(){
var thumbOver=$(this).find(“img”).attr(“src”);//获取图像url并将其分配给“thumbOver”
//在a标签上设置背景图像(拇指)
$(this.find(“a.thumb”).css({'background':'url('+thumbOver+')不重复中间底部'});
//将图像淡入0
$(this).find(“span”).stop(false,false).fadeTo('normal',0,function()){
$(this).hide()//淡入淡出后隐藏图像
});
});
},
函数(){//悬停在外时。。。
$(this).sides().each(函数(){
//将图像淡入1
$(this).find(“span”).stop(false,false).fadeTo('normal',1).show();
});
});
};
//当前平台
var platform=navigator.platform.toLowerCase();
//iDevice平台列表
变量iDevices={
“ipod”:没错,
“ipad”:没错,
“iphone”:真的吗
};
/*
//备选案文1:
//在DOM就绪状态下,执行除iPad之外的所有操作
$(函数(){
如果(平台!=“ipad”){
ready();
}
});
*/
//选择2
//仅当不是iDevice时执行
$(函数(){
if(!(iDevices中的平台)){
ready();
$(窗口)。调整大小(函数(){
视口大小();
});
}
});
})();
老实说,我从未做过android检测,但应该有类似的方法
希望有帮助!干杯。Keegan,谢谢你的回复。说到编码,我是个初学者。您能否演示如何将您提供的设备检查器实际放入上述代码中?我不知道如何把所有的东西结合在一起。谢谢。@MichaelHarmon,我对示例进行了编辑,以使其更加完整,您可以使用上面的整个脚本替换该脚本