DEMO
Ну вы должны ориентироваться на конкретные элементы с его последним символом, то и здесь, как это:
var clas="";//global variables
var lastChar="";
//attach mouseover event on element whose class starts with item
$('div[class^="item"]').on('mouseover',function(){
clas=$(this).attr('class'); //get the class of current element
lastChar = clas.substr(clas.length - 1); //get its last character
$(".pframe").find('.img'+lastChar).css('box-shadow', '0px 0px 43px 9px rgba(56,112,255,0.85)'); //find it in pframe and add css
}).on('mouseout',function(){
$(".pframe").find('.img'+lastChar).css('box-shadow', 'none');
//remove once mouseout
});
//attach mouseover event on element whose class starts with img
$('div[class^="img"]').on('mouseover',function(){
clas=$(this).attr('class');
lastChar = clas.substr(clas.length - 1)
$(".sidebar").find('.item'+lastChar).css('background-color', 'rgba(0,141,183,0.9)');
}).on('mouseout',function(){
$(".sidebar").find('.item'+lastChar).css('background-color', '');
});
UPDATE
UPDATED DEMO
Как отметил @roullie, это произойдет после 10-го предмета и после него, поскольку мы всегда получаем последний символ. Поэтому, если вы отредактируете свой html и добавьте класс, например item_1
, item_2
, а также img_1
и img_2
, вы можете использовать метод ниже, чтобы вытащить точный elements
.
var clas="";//global variables
var lastChar="";
//attach mouseover event on element whose class starts with item
$('div[class^="item_"]').on('mouseover',function(){
clas=$(this).attr('class'); //get the class of current element
lastChar = clas.split('_')[1]; //split _ and get its exact character to match
$(".pframe").find('.img_'+lastChar).css('box-shadow', '0px 0px 43px 9px rgba(56,112,255,0.85)'); //find it in pframe and add css
}).on('mouseout',function(){
$(".pframe").find('.img_'+lastChar).css('box-shadow', 'none');
//remove once mouseout
});
//attach mouseover event on element whose class starts with img
$('div[class^="img_"]').on('mouseover',function(){
clas=$(this).attr('class');
lastChar = clas.split('_')[1];
$(".sidebar").find('.item_'+lastChar).css('background-color', 'rgba(0,141,183,0.9)');
}).on('mouseout',function(){
$(".sidebar").find('.item_'+lastChar).css('background-color', '');
});
Есть более чем один 'img' ДИВ? Как 'img2',' img3' и т. Д. –
да, их несколько –
Итак, вы хотите совместить все div с изображениями с одинаковыми номерами? –